// Global Variables
var temp="", temp2="", temp3="", temp4="";
var working=false;
var intervalID 	= 0;
var includePath = "/includes/";
var videoSeason = 0;
var videoSortType = "";
// User Login 
function userLogin($type) {
	if($type=="facebook") {
		var fbAPI = FB.Facebook.apiClient;
		var fbUID = FB.Facebook.apiClient.get_session().uid;
		viewer  = fbAPI.fql_query('SELECT name, first_name, last_name, profile_url, sex, birthday_date, locale, email_hashes, proxied_email FROM user WHERE uid='+fbUID,
			function(results) {
				$("#loginbox").fadeOut();
				$.ajax({
					type: "POST",
					url: includePath+"ajax.php",
					data: "action=facebookuser&fbid="+fbUID+"&name="+results[0].name+"&firstname="+results[0].first_name+"&lastname="+results[0].last_name +"&profileurl="+ results[0].profile_url+"&locale="+ results[0].locale+"&sex="+results[0].sex+"&birthday="+results[0].birthday_date+"&email=",
					dataType: "html",
					success: function(returnedData) {
						//location.reload(true); 
						window.location="http://www.foxbytes.com.tr";
					}
				});
			}
  		);
		FB.XFBML.Host.parseDomTree(); 
	} else if($type=="fox") {
	} else {
	}
}
// Popup
function openCenteredWindow(url) {
    var width = 580;
    var height = 435;
    var left = parseInt((screen.availWidth/2) - (width/2), 10);
    var top = parseInt((screen.availHeight/2) - (height/2), 10);
    var windowFeatures = "width=" + width + ",height=" + height + ",status,resizable,left=" + left + ",top=" + top + "screenX=" + left + ",screenY=" + top;
    myWindow = window.open(url, "subWind", windowFeatures);
}
function showPreview(coords) {
	if (parseInt(coords.w, 10) > 0) {
		var rx = previewWidth / coords.w;
		var ry = previewHeight / coords.h;
		$("#x").val(coords.x);
		$("#y").val(coords.y);
		$("#w").val(coords.w);
		$("#h").val(coords.h);
		jQuery('.croppreview').css({
			width: Math.round(rx * imageWidth) + 'px',
			height: Math.round(ry * imageHeight) + 'px',
			marginLeft: '-' + Math.round(rx * coords.x) + 'px',
			marginTop: '-' + Math.round(ry * coords.y) + 'px'
		});
	}
}
function Jrefresh() {
	//$(".cropimage").load(function(){
		$(".cropimage").Jcrop({
			onChange: showPreview,
			onSelect: showPreview,
			boxWidth: 480,
			boxHeight: 480,
			aspectRatio: imageRatio,
			setSelect: [10,10,minWidth,minHeight],
			minSize: [minWidth, minHeight]
		});
	//});
	//imgEditObj = $.Jcrop(".cropbox",{	});
}
function trimString (str) {
	var	str = str.replace(/^\s\s*/, ''),
		ws = /\s/,
		i = str.length;
	while (ws.test(str.charAt(--i)));
	return str.slice(0, i + 1);
}
// Page is loaded
$(document).ready(function() {
// Set Dynamic Variables */
	var site = $("body").attr("id");
	var imageUrl = "http://image.foxbytes.com.tr/"+site+"/";
	var userImageUrl = "http://www.fic-test.com/"+site+"/Users/";
// Tabs
	$(".showtab").live("click", function(e) {
		e.preventDefault();
		temp = $(this).attr("rel");
		temp2= $(this).attr("rev");
		$("."+temp2).hide(function() {
			$("#"+temp).show();
			$("."+temp2+"Control").removeClass("active");
			$("#"+temp+"Control").addClass("active");
		});
	});
// Ajax Requests
	$("a.ajax").live("click", function(e) {
		e.preventDefault();
		temp = $(this);
		temp2 = $("form.ajax").serialize();
		temp3 = $("input[name='changelinks']").val();
		if(temp3!="") {
			$(temp3).find(".active").removeClass("active");
			temp.parent().addClass("active");
		}
		$.ajax({
			type: "POST",
			url: includePath+"ajax.php",
			data: "name="+temp.attr("name")+"&rel="+temp.attr("rel")+"&rev="+temp.attr("rev")+"&"+temp2,
        	dataType: "html",
        	success: function(returnedData) {
				temp2 = $("input[name='update']").val();
				$(temp2).html(returnedData);
			}
		});
	});
// Rating
	var rateState=0;
	$(".ratingModule a.rate").live("click", function(e) {
		e.preventDefault();
		temp = $(this).attr("rel");
		temp2= $("form.ratevalues").serialize();
		if(rateState===0) {
			$.ajax({
				type: "POST",
				url: includePath+"ajax.php",
				data: "action=rating&score="+temp+"&"+temp2,
				dataType: "html",
				success: function(returnedData) {
					if(returnedData) {
						rateState=1;
						$(".ratingResult").show();
					}
				}
			});
		}
	});
	$(".ratingModule a").hover(function() {
		if(rateState===0) {
			$(".ratingModule .active").removeClass("active");
			temp = $(this).parent();
			temp.addClass("active");
			temp.prevAll().addClass("active");
		}
	}, function() {
		if(rateState===0) {
			temp.removeClass("active");
			temp.prevAll().removeClass("active");
			$(".ratingModule").find(".activated").addClass("active");
		}
	});
// Show Div
	$("a.showdiv").live("click", function(e) {
		e.preventDefault();
		temp = $(this).attr("rel");
		$("#"+temp).fadeIn("fast");
	});
// Close Div 
	$("a.closediv").live("click", function(e) {
		e.preventDefault();
		temp = $(this).attr("rel");
		$("#"+temp).fadeOut();
	});
// Change Div
	$("a.changediv").live("click", function(e) {
		e.preventDefault();
		temp = $(this);
		if(temp.attr("rev")!=="") {
			$(temp.attr("rev")).fadeOut(function() {
				$(temp.attr("rel")).fadeIn();
			});
		}
	});
// Photo Gallery Image Change
	$("a.showimage").live("click", function(e) {
		e.preventDefault();
		temp = $(this).attr("rel");
		temp2= $(".photoSide img");
		temp3= $(".photoSide a");
		temp4= $(this).attr("rev");
		//alert("resim:"+temp+" link:"+temp4);
		temp2.fadeOut("fast", function() {
			temp2.attr("src",temp);
			temp3.attr("href",temp4);
			temp2.load(function() {
				temp2.fadeIn();
				$("a.colorbox").colorbox({href:temp4});
			});
		});
	});
// Form Validation
	$(".validate").each(function() {
		$(this).validate();
	});
// Form Validation
	$(".validatecustomerror").each(function() {
		$(this).validate({
			errorClass: "invalidfield",
			wrapper: "div",
			errorElement: "div"
		});
	});
// Ajax Upload and Crop 
	if ( $("#uploadajaxbutton").length > 0 ) {
		var uploadajax = new AjaxUpload('#uploadajaxbutton', {
			action: includePath+'ajaxupload.php',
			name: 'userfile',
			responseType: "json",
			//responseType: "html",
			onSubmit : function(file, ext){
				if (! (ext && /^(jpg|png|jpeg|gif)$/.test(ext))){
					$(".ajaxstatus").text('Invalid file! Only: .jpg, .gif, .png').show();
					return false;
				}
				$(".ajaxstatus").text('Yükleniyor... ').show();	
				$(".submit").attr("disabled","disabled");
			},
			onComplete : function(file, response){
				//alert(file);
				//alert(response.result);
				if(response.result==2) {
					$(".ajaxstatus").text("Maksimum dosya boyutunu aştınız.").show();	
					//alert("adawdwad");
					return false;
				} else {
					file = response.filename;
					file = file.substring(file.lastIndexOf('/')+1);
					//alert(response);
					//alert(file);
					if(response.result==1) {
						minWidth 	= $("#minwidth").val();
						minHeight 	= $("#minheight").val();
						imageWidth 	= response.width;
						imageHeight = response.height;
						if(imageWidth<minWidth) {
							$(".ajaxstatus").text("Resim boyutunuz en fazla "+minWidth+"px olmalıdır.").show();
							$(".submit").removeAttr("disabled");
							return false;
						} else if(imageHeight<minHeight) {
							$(".ajaxstatus").text("Resim boyutunuz en az "+minHeight+"px olmalıdır.").show();
							$(".submit").removeAttr("disabled");
							return false;
						} else {
							//$(".croparea").html('<div class="cropdiv"><img src="" class="cropimage" /><div class="buttonSave"><div class="buttonSaveLeft"></div><div class="buttonSaveMid"><input type="submit" value="Crop &amp; Save" /></div><div class="buttonSaveRight"></div></div></div>');
							$(".cropimage").attr("src",imageUrl+"Temp/"+file);
							$(".croppreview").attr("src",imageUrl+"Temp/"+file);
							//alert(imageUrl+"Temp/"+file);
							$('.ajaxstatus').html('Lütfen fotoğrafınızı biçimlendirin.').show();
							previewWidth = $("#previewwidth").val();
							previewHeight = $("#previewheight").val();
							imageRatio = previewWidth/previewHeight;
							$(".croppre div").css({"width":previewWidth+"px", "height":previewHeight+"px"});
							$("#f").val(file);
							$(".submit").removeAttr("disabled");
							//$(".ajaxupload").slideUp();
							$(".croparea").slideDown();
							Jrefresh();
						}
					} else {
						$(".ajaxstatus").text(response.result).show();	
						//alert("adawdwad");
						return false;
					}
				}
			}		
		});
	}
	$("#ajaxavatar").submit(function(e){
		e.preventDefault();
		temp = $(this);
		temp = temp.serialize();
		$.ajax({
			type: "POST",
			url: includePath+"ajax.php",
			data: "action=ajaxavatar&"+temp,
        	dataType: "html",
        	success: function(returnedData) {
				//alert(returnedData);
				temp3 = trimString(returnedData);
				$(".croparea").slideUp();
				$(".croppreview").attr("src","http://image.foxbytes.com.tr/Users/180/"+temp3);
				$(".croppreview").attr("width","180");
				$(".croppreview").attr("height","180");
				$(".croppreview").css({"width":"180px", "height":"180px", "margin-left":"0px", "margin-top":"0px"});
				temp2 = $("#profilePictureChangeAlert").text();
				$(".ajaxstatus").text(temp2);
			}
		});
	});	
// Clickable Links
	$(".clickable").live("click", function(e) {
		temp = $(this).find("a").attr("href");
		document.location = temp;
	});
	$(".clickable").hover(function() {
			$(this).addClass("hover");
		}, function() {
			$(this).removeClass("hover");
	});
// Colorbox
	$("a.colorbox").colorbox();
});