// JavaScript Document
var skyline = {};
var sliderParams = {};
var isMobile = navigator.userAgent.indexOf('iPhone') >= 0 || navigator.userAgent.indexOf('iPad') >= 0 || navigator.userAgent.indexOf('Android') >= 0 || navigator.userAgent.indexOf('Mobile Safari') >= 0;
var isIE7 = navigator.appVersion.indexOf("MSIE 7") > 0;
var isIE6 = navigator.appVersion.indexOf("MSIE 6") > 0;
var slideFunction;var slidesWidth; var skip_Intro;
$(function(){
	if (isMobile) {
		$("head").append('<link type="text/css" href="home/css/jquery.jscrollpane.css" rel="stylesheet" media="all" /><script type="text/javascript" src="home/scripts/jquery.jscrollpane.min.js"></script>')	
	}

	if (skyline.replaceFonts!="" && window.Cufon) {
		if(!isIE6&&!isIE7) skyline.replaceFonts+=",.window-heading,.inner-heading,.dropcap"
		Cufon.replace(skyline.replaceFonts)	
	}
	/*----------IE6 & IE7----------*/
	if(isIE6) {
		$(".module:eq(0)").css({background:'url("home/images/layouts/mod-mid.png") repeat-y scroll left top transparent'})
		$(".module:eq(1)").css({background:'url("home/images/layouts/mod-mid.png") repeat-y scroll -280px top transparent'})
		$(".module:eq(2)").css({background:'url("home/images/layouts/mod-mid.png") repeat-y scroll -560px top transparent'})
	}
	/*----------Feeds----------*/
	$('#forum-feed').miniFeed('http://www.iamiraqi.com/forum/RSS_topic_feed.asp',{
		phpRepeater:"home/scripts/getfeed.php",
		getFeedTitle:false,
		limit:10,
		nextLinkText:"Read more..",
		getItemSummary : false,
		getItemDescription : false,
		getItemDate : false,
		getItemLink: true,
		timeout : 20000
		},function(){
			$("#forum-feed ul").append('<li class="feed-li"></li>')
			$("#forum-feed ul").append('<li class="feed-li"></li>')
			$("#forum-feed .feed-li:eq(0)").clone().appendTo("#forum-feed ul");
			$("#forum-feed .feed-li:eq(1)").clone().appendTo("#forum-feed ul");
			$("#forum-feed .feed-li:eq(2)").clone().appendTo("#forum-feed ul");
			$("#forum-feed .feed-li:eq(3)").clone().appendTo("#forum-feed ul");
			$("#forum-feed .feed-li").fadeIn(300,function(){
				$(this).css({display:"block"})
				if(isIE6) fixModulesHeight()
			});
			$("#forum-feed .feed-li:eq(0)").addClass("active-ff");
			$("#forum-feed a").each(function(){
				$(this).addClass("new-window iframe showURL")
				.attr({
					"href":"?link="+encodeURIComponent($(this).attr("href"))+"&showURl",
					"rel":"900",
					"rev":"500"
				})
			});
			$("#forum-feed .feed-li").each(function(){
				var fFfeedLink = $(this).find(".feedLink").attr("href");
				$(this).find(".feedTitle").wrapInner('<a class="new-window iframe showURL" href="'+fFfeedLink+'"></a>')
			})
			var numberOfFFeeds = $("#forum-feed .feed-li").length
			initLinksIn("#forum-feed")
			var activeFFIndex = $(".active-ff").index();
			setInterval (function(){
				var moveHeight;
				if (isIE7|| isIE6) moveHeight = 370;
				else moveHeight = 360;
				$("#forum-feed li:gt(0)").animate({top:"-="+(moveHeight)+"px"},800)
				$("#forum-feed .feed-li:eq(0)").animate({top:"-="+(moveHeight)+"px"},800,function(){
					$(".feed-li").eq(activeFFIndex).removeClass("active-ff")
					$(".feed-li").eq(activeFFIndex+4).addClass("active-ff")
					activeFFIndex = $(".active-ff").index();
					if ($(".active-ff").index() == numberOfFFeeds-4) {
						
						$("#forum-feed .feed-li").css({top:0});
						$(".active-ff").removeClass("active-ff")
						$("#forum-feed .feed-li:eq(0)").addClass("active-ff");
					}	
				})
			},5000)

			$('#news-feed').miniFeed('http://rss.news.yahoo.com/rss/iraq',{
				phpRepeater:"home/scripts/getfeed.php",
				getFeedTitle:false,
				limit:10,
				nextLinkText:"Read more..",
				timeout : 20000
			},function(){
				$('#news-feed li:eq(0)').fadeIn(300)
				if(isIE6) fixModulesHeight()
				var i = 0;
				setInterval(function(){
						$('#news-feed li:eq('+i+')').fadeOut(300);
						i++;
						if(i==$('#news-feed li').length) i = 0;
						$('#news-feed li:eq('+(i)+')').delay(400).fadeIn(300);
				},5000)
				
			});	
	});

	/*----------Twitter Feed----------*/
	if(skyline.twitter.username!="") $("#tweets").idTwitter(skyline.twitter);

	/*----------Toolbar----------*/
	$(".toolbar-icon").hover(
		function(){
			if(isFade()) {
				$(".toolbar-icon").not($(this)).stop(false,false).fadeTo(300,0.5)
			}
			$(this).find("img").animate({
				marginTop:10
			},300)
				
		},
		function(){
			if(isFade()) {
				$(".toolbar-icon").not($(this)).stop(false,false).fadeTo(300,1)
			}
			$(this).find("img").animate({
				marginTop:0
			},300)
		}
	)
	/*----------Ajax Loader----------*/
	$("body").ajaxStart(function(){
		showLoader();
	})
	$("body").ajaxComplete(function(){
		hideLoader();
	})
	/*----------Links Initialization----------*/
	initLinksIn("body");
})
function isFade() {
	if (skyline.noFadeInIE6_8 && isIE6_8()) return false;
	else return true;
}
function isIE6_8() {
	if(navigator.appVersion.indexOf("MSIE 6") > 0 || navigator.appVersion.indexOf("MSIE 7") > 0 || navigator.appVersion.indexOf("MSIE 8") > 0) return true;	
}
function initLinksIn(parent) {
	if (parent=="body") parent = "";
	/*----- New Window Links -----*/
	$(parent+" a.new-window").click(function(e){
		e.preventDefault();
		var contentType, windowTollbar, windowColor;
		/*----- Window Content -----*/
		if($(this).hasClass("iframe")) contentType = "iframe";
		else if($(this).hasClass("withImg")) contentType = "image";
		else contentType = "text";
		/*----- Window Color & Toolbar -----*/
		if($(this).attr("class").indexOf("set(")>=0) {
			windowColor = $(this).attr("class").split("set(")[1].split(",")[0]
			windowToolbar = $(this).attr("class").split("set(")[1].split(",")[1].split(")")[0]
		}
		else {
			windowColor = skyline.windowColor
			windowToolbar = contentType=="image"?skyline.imageWindowToolbar:skyline.contentWindowToolbar
		}
		newWindow({
			url : contentType=="iframe"?decodeURIComponent($(this).attr('href').split("link=")[1].split("&")[0]):$(this).attr('href').split("page=")[1],
			width : $(this).attr("rel"),
			height : $(this).attr("rev"),
			contentType : contentType,
			title:$(this).hasClass("withImg")?$(this).attr('title'):"",
			left : skyline.windowFromLink==true?$(this).offset().left:0,
			top : skyline.windowFromLink==true?$(this).offset().top:0,
			windowColor : windowColor,
			windowToolbar : windowToolbar,
			showURL : $(this).hasClass("showURL")?true:false
		})
	})
	/*----- Tooltips -----*/
	$(parent+" .hasTip").hover(
		function(){
			var toolTip = $("#tooltip");
			if ($(this).attr("title")) {
				var tipText = $(this).attr("title");
				toolTip.html(tipText)
				var ttLeft = $(this).offset().left-20-50;
				var ttTop = $(this).offset().top - toolTip.height() - 30 - 120;
				toolTip.css({
					left:ttLeft,
					top:ttTop
				})
				.animate({
					top:ttTop+120,
					left:ttLeft+50
				},{queue:false,duration:skyline.toolTipShowTime/2})
				.fadeIn(skyline.toolTipShowTime)
			}
		},
		function(){
			$("#tooltip").stop(true,true).hide().html("")
		}
	)
	/*----- Button -----*/
	$(parent+" .button").wrap('<span class="button-l"><span class="button-m"></span></span>');
	/*------ Window Content ------*/
	if (parent!="") {
		var windowID = $(parent).attr('id').split("window_")[1];
		$(parent+" .closeWindow").click(function(e){
			e.preventDefault();
			closeWindow(windowID)
		})
		$(parent+" a.openHere").click(function(e){
			e.preventDefault();
			replaceWindowContent({
				windowID : windowID,
				url : $(this).attr('href'),
				width : $(this).attr("rel"),
				height : $(this).attr("rev")
			})
		})
	}
}
function showLoader(){
	$(".ajax-loader").css({top:$(window).scrollTop()+200}).show();	
}
function hideLoader(){
	$(".ajax-loader").hide();
}
function expand(id,windowWidth,windowHeight){
	$("#window_"+id).resizable("enable")
	.animate({
		width: windowWidth,
		height: windowHeight
	},{queue:false, duration:skyline.expandWindowTime})
	.find("iframe").animate({height:windowHeight-80},skyline.expandWindowTime)
	if (skyline.withScrollBar && $("#window_"+id).find("iframe").length==0) {
		$("#window_"+id).find(".w-mm").css({overflow:"auto"})	
	}
	$("#window_"+(id)+" .w-ml").animate({width:windowWidth-20,height:windowHeight-40},skyline.expandWindowTime)
	$("#window_"+id+" .toolbar .expand-icon").remove()
	$("#window_"+id).find(".maximize-icon").show()
	$('<div class="minimize-icon" title="Minimize Window" onclick="minimize('+id+')"></dic>').insertBefore("#window_"+id+" .close-icon")
}
function maximize(id){
	$("#window_"+id)
	.animate({
		width: $(window).width(),
		height: $(window).height(),
		left:0,
		top:$(window).scrollTop()
	},{queue:false, duration:700})
	.find("iframe").animate({height:$(window).height()-80,width:"100%"},700)
	if (skyline.withScrollBar && $("#window_"+id).find("iframe").length==0) {
		$("#window_"+id).find(".w-mm").css({overflow:"auto"})	
	}
	$("#window_"+(id)+" .w-ml").animate({width:$(window).width()-20,height:$(window).height()-40},700)
}
function minimize(id) {
	$("#window_"+id).animate({width:isIE6?"225px":"175px",height:"50px",left:"10px"},{queue:false, duration:skyline.minimizeWindowTime,complete:function(){
		$(this).addClass("minimized")
		if(isFade()) $(this).fadeTo(300,0.3)
		$(this).resizable("disable")
	}})
	.children(".w-ml").animate({height:"10px",width:isIE6?"205px":"155px"},{queue:false, duration:skyline.minimizeWindowTime})
	.find("iframe").animate({height:"120px",width:"100%"},{queue:false, duration:skyline.minimizeWindowTime})
	$("#window_"+id).find(".maximize-icon").hide()
	if (skyline.withScrollBar && $("#window_"+id).find("iframe").length==0) {
		$("#window_"+id).find(".w-mm").css({overflow:"visible"})
	}
	$("#window_"+id+" .toolbar .minimize-icon").remove()
	$('<div class="expand-icon" title="Expand Window" onclick="expand('+id+','+$("#window_"+id).width()+','+$("#window_"+id).height()+')"></dic>').insertBefore("#window_"+id+" .close-icon")
}
function closeWindow(id) {
	$("#window_"+id).remove()
}
function newWindow(params){
	params.width = params.width || skyline.defaultWindowWidth;
	params.contentType = params.contentType||"text";
	params.left = params.left||0;
	params.top = params.top||0;
	params.title = params.title || "";
	params.windowColor = params.windowColor||skyline.windowColor;
	var windowID;
	if ($(".window:last").index()=="-1") windowID = 0
	else windowID = $(".window:last").attr("id").split("window_")[1]*1+1;
	if (params.contentType!="image") {
		params.windowToolbar = params.windowToolbar || skyline.contentWindowToolbar;
		jQuery.get("home/window.tmpl.php",{
				id : windowID, 
				url : params.url, 
				contentType : params.contentType, 
				width: params.width
			},function(tmpl){
			$("#windows").append(tmpl)
			var addedWindow = $("#window_"+windowID)
			var windowTop = $(window).scrollTop()+100;
			var windowHeight = params.height||addedWindow.outerHeight()*1;
			addedWindow.addClass(params.windowColor)
			if(window.Cufon !== undefined) Cufon.refresh();	
			addedWindow.find(".toolbar").addClass(params.windowToolbar);
			if (params.windowToolbar=="os") {
				addedWindow.find(".toolbar").wrap('<div class="t-os-l"><div class="t-os-r"></div></div>')	
			}
			if (skyline.rotateWindows) addedWindow.css3({transform:"rotate(45deg)",time:0})
			if(params.contentType!="iframe")addedWindow.find(".w-content").css({display:"none"})
			addedWindow.css({
				width:290,
				left: params.left,	
				top: params.top,
				height: 50,
				opacity:0.1,
				display: "block",
				filter: "alpha(opacity=10)"
			})
			.animate({
				left: ($(window).width() - params.width)/2+"px",	
				top: $(window).scrollTop()+100,
				height: windowHeight*1+10,
				width: params.width,
				opacity:1
			},skyline.openWindowTime,function(){
					 addedWindow.css({filter:""})
					 if(params.contentType!="iframe") addedWindow.find(".w-content").delay(300).fadeIn(600,function(){
					 	if (isMobile) $(".w-mm").jScrollPane({animateScroll: true})
					 })
					 else {
						if(params.showURL) {
							var iFrameURL = addedWindow.find("iframe").attr("src");
							addedWindow.append('<input type="text" class="iframe-url" value="'+iFrameURL+'" />')	
							addedWindow.find(".iframe-url").click(function(){$(this).select()})
							addedWindow.find("iframe").load(function(){
								addedWindow.find(".iframe-url").attr({
									"value":(isIE6||isIE7) ? $(this)[0].contentWindow.location : $(this)[0].contentDocument.location
								})
							})
						} 
					 }
					 if(isFade())$(".window:lt("+addedWindow.index()+")").fadeTo(300,0.8)
			})
			.find(".w-mm").css({overflow:(skyline.withScrollBar&&params.contentType=="text")?"auto":""}).end()
			.find(".w-ml").animate({height:windowHeight-30},skyline.openWindowTime).end()
			.find("iframe").animate({height:windowHeight-60},skyline.openWindowTime).end()
			.draggable({ handle: 'div.drag-icon' })
			.resizable({ 
				alsoResize: '#window_'+windowID+' .w-ml, #window_'+windowID+' iframe',
				minHeight: 50,
				minWidth:isIE6?225:175 
			})
			.mousedown(function(){
				if($(this).css("opacity")!=1 && isFade()) {
					$(this).css({"z-index":3000}).fadeTo(300,1,function(){$(this).css({"filter":""})})
					$(this).nextAll(".window").css({"z-index":2000}).fadeTo(300,0.8)
					$(this).prevAll(".window").css({"z-index":2000}).fadeTo(300,0.8)
				}
				if(!isFade()) {
					$(this).css({"z-index":3000})
					$(this).nextAll(".window").css({"z-index":2000})
					$(this).prevAll(".window").css({"z-index":2000})
				}
			})
			initLinksIn("#window_"+windowID);
		})
	}
	else {
		params.windowToolbar = params.windowToolbar || skyline.imageWindowToolbar;
		jQuery.get("img.tmpl.php",{id : windowID, url : params.url, title : params.title},function(tmpl){
			showLoader();
			$("#windows").append(tmpl)
			var addedWindow = $("#window_"+windowID);
			if (skyline.rotateWindows) addedWindow.css3({transform:"rotate(45deg)",time:skyline.openWindowTime})
			addedWindow.find(".toolbar").addClass(params.windowToolbar);
			if (params.windowToolbar=="os") {
				addedWindow.find(".toolbar").wrap('<div class="t-os-l"><div class="t-os-r"></div></div>')	
				.end().find(".w-content").css({borderTop:"none"})
			}
			var windowWidth = addedWindow.width()
			addedWindow.draggable({ handle: 'div.toolbar' });
			addedWindow.css({
				width:365,
				left: params.left,	
				top: params.top
			})
			.addClass(params.windowColor)
			if(window.Cufon !== undefined) Cufon.refresh()
			$("#window_"+windowID+" img.window-image").css({width:345})
			$("#window_"+windowID+" .window-image")[0].onload = function(){
				hideLoader();
				addedWindow.animate({
					left: ($(window).width() - windowWidth+20)/2+"px",	
					top: $(window).scrollTop()+100,
					width: windowWidth
				},{queue : false, duration : skyline.openWindowTime,complete:function(){
					addedWindow.css({filter:""})
				}})
				if (skyline.rotateWindows) addedWindow.css3({transform:"rotate(0deg)", time:skyline.openWindowTime})
				addedWindow.fadeIn(skyline.openWindowTime)
				
				if(isFade()) $(".window:lt("+addedWindow.index()+")").delay(skyline.openWindowTime+200).fadeTo(300,0.3)
				$("#window_"+windowID+" .window-image").animate({
					width:windowWidth-20
				},skyline.openWindowTime)
				$("#window_"+windowID+" .image-title").delay(skyline.openWindowTime+300).slideDown(300)
			}
			addedWindow.mousedown(function(){
				if($(this).css("opacity")!=1 && isFade()) {
					$(this).css({"z-index":3000}).fadeTo(300,1,function(){
						$(this).css({"filter":""})
					})
					$(this).nextAll(".window").css({"z-index":2000}).fadeTo(300,0.5)
					$(this).prevAll(".window").css({"z-index":2000}).fadeTo(300,0.5)
				}
				if(!isFade()) {
					$(this).css({"z-index":3000})
					$(this).nextAll(".window").css({"z-index":2000})
					$(this).prevAll(".window").css({"z-index":2000})
				}
			})
			$("#window_"+windowID+" .zoom-in-icon").click(function(){
				addedWindow.animate({
					width:"+=100px",
					left:"-=50px"
				},300)
				$("#window_"+windowID+" .window-image").animate({width:"+=100px"},300)	
			})
			$("#window_"+windowID+" .zoom-out-icon").click(function(){
				if (addedWindow.width()<=500) return;
				addedWindow.animate({
					width:"-=100px",
					left:"+=50px"
				},300);
				$("#window_"+windowID+" .window-image").animate({width:"-=100px"},300)	
			})
		})
	}
}
function replaceWindowContent(params) {
	var replacedWindow = $("#window_"+params.windowID);
	var replacedContent = replacedWindow.find(".w-content");
	replacedContent.fadeOut(300,function(){
		jQuery.get(params.url,function(newContent){
			replacedContent.html(newContent);
			if(window.Cufon !== undefined) Cufon.refresh();
			initLinksIn("#window_"+params.windowID);
			if(params.width && params.height) {
				replacedWindow.animate({
					width:params.width,
					left:($(window).width()-params.width)/2,
					height:params.height
				},skyline.openWindowTime)
				replacedWindow.find(".w-ml").animate({
					height:params.height-40,
					width:params.width-20
				},skyline.openWindowTime)
			}
			replacedContent.delay(300+skyline.openWindowTime).fadeIn(300)
		})	
	})
}
(function($) {
	$.fn.css3 = function(params) {
		if (skyline.disableCSS3Transforms) return this;
		var transform = params.transform;
		var time = params.time;
		return this
		.css({
			"-o-transform":transform,
			"-o-transition-duration":(time/1000)+"s",
			"-o-transition-property":"-o-transform",
			"-webkit-transform":transform,
			"-webkit-transition-duration":(time/1000)+"s",
			"-webkit-transition-property":"-webkit-transform",
			"-moz-transform":transform,
			"-moz-transition-duration":(time/1000)+"s",
			"-moz-transition-property":"-moz-transform",
			"transform":transform,
			"transition-duration":(time/1000)+"s",
			"transition-property":"transform"
		})
	}
})(jQuery)
function fixModulesHeight() {
	$(".module").css({height:""})
	var moduleHeight=0;
	for (var i=0;i<$(".module").length;i++) {
		var iModuleHeight = $(".module").eq(i).height()
		if(iModuleHeight > moduleHeight) moduleHeight = iModuleHeight;
	}
	$(".module").height(moduleHeight)
}
function validateText(fieldId,length) {
	field = $("input#"+fieldId);
	text = field.val();
	m_length = ($.trim(text)).length;
	if (m_length < length) field.addClass("required");
	else field.removeClass("required");
}
function validateMessage() {
	message = $("textarea#contact_message").val();
	m_length = ($.trim(message)).length;
	if (m_length < 10) $("textarea#contact_message").addClass("required");
	else $("textarea#contact_message").removeClass("required");
}
function validateEmail() {
	var email = $("input#contact_email").attr("value");
	var re = /^\w+([\.-]?\w+)*@(((([a-z0-9]{2,})|([a-z0-9][-][a-z0-9]+))[\.][a-z0-9])|([a-z0-9]+[-]?))+[a-z0-9]+\.([a-z]{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum|mobi|travel))$/i;
	if(re.test(email)) $("input#contact_email").removeClass("required");
 	else $("input#contact_email").addClass("required");
}
function sendMessage() {
	validateText("contact_name",6)
	validateText("contact_subject",6)
	validateEmail()
	validateMessage()
	if (!$("#contact_email,#contact_message,#contact_name,#contact_subject").hasClass("required")) {
		name = $("#contact_name").attr("value");
		email = $("#contact_email").attr("value");
		subject = $("#contact_subject").attr("value");
		message= $("#contact_message").val().replace(/<\/?[^>]+>/gi, '');
		jQuery.post("home/scripts/send_message.php",{name : name, email : email, subject : subject, message : message},
			function (status) {
				$(".contact-form").fadeOut(600)
				if(status==1) {
					$(".message-status").html('<h4 class="notification"><span class="message-sent"></span>Thanks! Your message has been successfully sent.</h4>').delay(600).fadeIn(600)
					
				}
				else $(".message-status").html('<h4 class="notification"><span class="message-error"></span> Sorry! Error occured. Try again later please.</h4>').delay(600).fadeIn(600)
			}
		);
	}
}
(function($) {
	$.fn.idTwitter = function(a) {
		var tweetContainer = this;
		$.getJSON("http://api.twitter.com/1/statuses/user_timeline.json?screen_name="+a.username+"&count="+a.numberOfTweets+"&include_entities=true&include_rts=true&callback=?",
		function(tweetData){
			$.each(tweetData, function(i,tweet) {
				var tweetDate = tweet.created_at.substr(0,20);
				var tweetText = tweet.text;
				for (var i =0; i<tweet.entities.user_mentions.length; i++) {
					var mentioned = tweet.entities.user_mentions[i].screen_name;
					tweetText = tweetText.replace('@'+mentioned,'<a href="http://twitter.com/'+mentioned+'">@'+mentioned+'</a>')	
				}
				for (var i =0; i<tweet.entities.urls.length; i++) {
					var uRL = tweet.entities.urls[i].url;
					tweetText = tweetText.replace(uRL,'<a href="'+uRL+'">'+uRL+'</a>')	
				}
				var readyTweet = a.tweetFormat.replace(/%username/g,a.username)
					readyTweet = readyTweet.replace(/%tweetDate/g,tweetDate)
					readyTweet = readyTweet.replace(/%tweetText/g,tweetText)
				tweetContainer.append(readyTweet);
				if(isIE6) fixModulesHeight()
			})
			
		})
	}
})(jQuery)
