$(function(){
	//QueryLoader.init();
	//$("body").fadeIn(2000).animate({'opacity':'1'},1000).delay(500).css({'visibility':'visible'});
	var theWindow = $(window),
	$bg = $(".bg"),
	aspectRatio = $bg.width() / $bg.height();
	
	function resizeBg(){
	  windowHeight = $(window).height();
	  windowfifty = (windowHeight * 0 ) / 100;
	 //arrowPos = windowHeight - 100 ;

      $(".scrollWrapper").css({'margin-top': windowfifty +'px'});
	  $(".slides_control").css({'height': windowHeight +'px'});
	  //$(".arrows").css({'margin-top': arrowPos +'px'});


		var windWidth =  theWindow.width();
	   $(".bg").css({width: windWidth +'px'});
		
		if((theWindow.width() / theWindow.height()) < aspectRatio ){
			$bg.removeClass().addClass('bgheight');
		}else{
			$bg.removeClass().addClass('bgwidth');
		}
	}
	
	//Inner Galleries
	$("a[rel='photoGallery']").fancybox({
		'transitionIn' : 'none',
		'transitionOut' : 'none',
		'titlePosition' : 'over',
		'titleFormat' : function(title, currentArray, currentIndex, currentOpts) {
			return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
		}
	});

	
	theWindow.resize(function(){
		resizeBg();
	}).trigger("resize");

	$("ul.tabs").tabs("div.panes > div");
	$("ul.social-tabs").tabs("div.social-panes > div");
	
	$("#browsable, #browsable2, #browsable3").scrollable();
	
	var browsableLength = $("#browsable .items div.panel").length;
	var browsable2Length = $("#browsable2 .items div.panel").length;
	var browsable3Length = $("#browsable3 .items div.panel").length;  // News Page 

	var nos = 0;
	var nos2 = 0;
	var nos3 = 0; // News Page 

	$('#browsable .items div.panel').each(function() {
		nos++ ;	
		$(this).append('<div class="pager"><label class="current">' + nos +'<sub> / </sub> </label>' + browsableLength  + '</div>');
	});

	$('#browsable2 .items div.panel').each(function() {
		nos2++ ;
		$(this).append('<div class="pager"><label class="current">' + nos2 +'<sub> / </sub> </label>' + browsable2Length  + '</div>');
	});

	// News Page 
	$('#browsable3 .items div.panel').each(function() {
	nos3++ ;
	$(this).append('<div class="pager"><label class="current">' + nos3 +'<sub> / </sub> </label>' + browsable3Length  + '</div>');

	});

	$("#scroll").smoothDivScroll({});

   $(".info").hover(function(){
	 $(this).children('.info-description').stop().fadeIn().animate({'opacity':1});
	// alert('ed');
	 },		 
	 function(){
		 $(this).children('.info-description').stop().fadeOut().animate({'opacity':0});
	 });
   
   $('.block').hover(function(){
	   if(!$(this).hasClass('halfAccordion')){
		   if(!$(this).hasClass('smallerSizeAccordion')){
			   if($(this).hasClass('mList')){
				   $(this).removeClass("top-180").addClass("top-mList");
			   }else{
				   $(this).stop().addClass('top-reset' , "fast");
			   }
		   }
	   }else{
		   $(this).stop().addClass('top-reset-half' , "fast");
	   }
   },function (){
	   if(!$(this).hasClass('halfAccordion')){
		   if(!$(this).hasClass('smallerSizeAccordion')){
			   if($(this).hasClass('mList')){
				   $(this).removeClass("top-mList").addClass("top-180");
			   }else{
				   $(this).stop().removeClass('top-reset').css({'margin-top':''});
			   }
		   }
	   }else{
		   $(this).stop().removeClass('top-reset-half').css({'margin-top':''});
	   }
   });	
   	
	//Play Music
   $('.list li a.play').live('click', function(){
		$('.played').removeClass('played').addClass('play');
		$(this).removeClass('play').addClass('played');
		songUrl = $(this).attr('rel');
		
		_gaq.push(['_trackEvent', 'Music Lists', 'Play', songUrl]);
		
		$('#radioPlayer').each(function (){
	        try{
	            this.playPause(false);
	        }catch(ex){
	        }
	    });
	    
		$('#hiddenPlayer').each(function (){
	        try{
	            this.playMusic(songUrl);
	        }catch(ex){
		        alert('Flash Fonksiyon bulunamadı hatası: RadioPlayer');
	        }
	    });
	});
	
	$('.list li a.played').live('click', function(){
		$(this).removeClass('played').addClass('play');
		mState = getPlayerState();
		_gaq.push(['_trackEvent', 'Music Lists', 'Pause', songUrl]);
		$('#radioPlayer').each(function (){
	        try{
	        	if(mState == 'true'){
	        		this.playPause(true);
	        	}
	        }catch(ex){
		        alert('Flash Fonksiyon bulunamadı hatası: MusicPlayer');
	        }
	    });
		$('#hiddenPlayer').each(function (){
	        try{
	            this.stopMusic();
	        }catch(ex){
	        	alert('Flash Fonksiyon bulunamadı hatası: RadioPlayer');
	        }
	    });
	});
		
	$('.likeBtn').live('click', function(){
		rel = $(this).attr('rel');
		if($.cookie('liked'+rel) == null){
			$.cookie('liked'+rel, rel);
			$.ajax({
				url: baseURL+'programlar/like/'+rel, 
				success:function(data){ 
					$('.begendimTotal').text('+'+data);
					$('.likeBtn').removeClass('likeBtn');
				}
			});
		}
		
		_gaq.push(['_trackEvent', 'Programs', 'Liked', rel]);
	});
	
	if($('.block').length > 4){
		$('.scrollingHotSpotRight').fadeIn();
	}
	
	//Mouse Scroll
	scrollCount = 1;
	$('body').bind('mousewheel', function(event, direction){
		if($('.block').length > 4){
			if(direction > 0 && scrollCount+1 != 1){
		        scrollCount--;
		        $("#scroll").smoothDivScroll("moveToElement", "number", scrollCount);
			}else{
				scrollCount++;
		        $("#scroll").smoothDivScroll("moveToElement", "number", scrollCount);
			}
		}
	});
	
	$('.pollContainer form input').live('click', function(){
		formSubmitUrl = $(this).parent().parent().parent().parent().attr('action');
		form = $(this).parent().parent().parent().parent();
		elem = form.parent();
		pollID = form.attr('rel');
		_gaq.push(['_trackEvent', 'Polls', 'Vote', 'Poll Vote']);
		
		if($.cookie('poll'+form.attr('rel')) == null){
			$.cookie('poll'+form.attr('rel'), $(this).val(), '/');
			$.ajax({
				type: "POST",
				url: formSubmitUrl,
				data: $(this).serialize(),
				success: function(){
					elem.load(baseURL+'anket/sonuc/'+pollID);
				}
			});
		}else{
			elem.load(baseURL+'anket/sonuc/'+pollID);
		}
	});
	
	$('.chatzonePoll form input').live('click', function(){
		_gaq.push(['_trackEvent', 'Polls', 'Vote', 'Poll Vote']);
		
		form = $(this).parent().parent().parent().parent();
		pollID = form.attr('rel');
		if($.cookie('chatzone'+form.attr('rel')) == null){
			$.cookie('chatzone'+form.attr('rel'), $(this).val(), '/');
			$.ajax({
				type: "POST",
				url: baseURL+'inoutVote/doVote/mk/'+pollID,
				data: $(this).serialize(),
				success: function(data){
					$('.chatzonePoll').html(data);
				}
			});
		}else{
			alert('Yalnızca bir kere oy verebilirsiniz');
		}
	});
	
	$('.btnPopup').live('click', function(){
		newsWindow = window.open($(this).attr('href'), "newsPlayer", "location=0,status=0,scrollbars=0,width=652,height=452");
		newsWindow.moveTo(0, 0);
		
		return false;
	});
	
	//Ajax Form Submit
	$('.registerForm form').live('submit', function(){
		var errors;
		var myElem;
		
		$(this).find('.required').each(function(){
			if(jQuery.trim($(this).val()).length == 0){
				$(this).addClass('error');
				$(this).prev('label').addClass('error');
				errors = true;
			}else{
				$(this).prev('label').removeClass('error');
				$(this).removeClass('error');
				
				if($(this).attr('type') == 'checkbox'){
					if(!$(this).is(':checked')){
						errors = true;
						$(this).addClass('error');
						$(this).prev('label').addClass('error');
					}
				}
				if(errors != true){
					errors = false;
				}else{
					errors = true;
				}
			}
			
		});
		
		if(!errors){
			overlayElem = $(this).next('mOverlay');
			overlayElem.fadeIn();
			//Send it!
			formSubmitUrl = $(this).attr('action');
			form = $(this);
			_gaq.push(['_trackEvent', 'Forms', 'Submit', formSubmitUrl]);
			$.ajax({
				type: "POST",
				url: formSubmitUrl,
				data: $(this).serialize(),
				success: function(data){
					//alert(data);
					overlayElem.fadeOut();
					form.fadeOut('fast', function(){
						form.next('.successForm').fadeIn('slow', function(){
							form.parent().parent().animate({top: '310px'}, 500);
						});
					});
				}
			});
		}
		
		return false;
	});
	
  	$('.scrollToContent').live('click', function(){
  		$('.subCurrentElem').removeClass('subCurrentElem');
  		$(this).addClass('subCurrentElem');
  		myElemId = $('#'+$(this).attr('rel')).index();
  		$("#scroll").smoothDivScroll("moveToElement", "number", myElemId);
	});
  	
  	$('.btnCloseLightBox').live('click', function(){
  		$('.mOverlay, .facebookPopup').fadeOut();
  	});
  	
  	$('.featured-content').live('click',function(){
  		if($(this).find('a').length > 0){
  			document.location.href = $(this).find('a').attr('href');
  		}else{
  			return false;
  		}
  	});
});

function changePlayerState(){
	$('.played').removeClass('played').addClass('play');
	$('#radioPlayer').each(function (){
        try{
            this.playPause(true);
        }catch(ex){
        }
    });
}

function videoPlayer(fileID, fileType){
	_gaq.push(['_trackEvent', 'Popup Player', 'Play', fileID+'/'+fileType]);
	$('#radioPlayer').each(function (){
        try{
        	this.playPause(false);
        }catch(ex){
        }
    });
	playWindow= window.open(baseURL+"PowerPlayer/1/"+fileID+"/"+fileType, "playWindow", "location=0,status=0,scrollbars=0,width=790,height=520");
	playWindow.moveTo(0, 0);
}

function openBigPlayer(){
	$('#radioPlayer').each(function (){
        try{
            this.playPause(false);
        }catch(ex){
        }
    });
	bigPlayer = window.open(baseURL+"PowerPlayer", "bigPlayer", "location=0,status=0,scrollbars=0,width=790,height=520");
    bigPlayer.moveTo(0, 0);
}

function openFestive(){
	$('.mOverlay').fadeIn();
	$('.festiveSchedule').fadeIn();
}

function setPlayerState(state){
	$.cookie('radioPlayer', state, '/');
}

function getPlayerState(){
	return $.cookie('radioPlayer');
}

function openWebcam(){
	liveCamWindow = window.open(baseURL+"PowerWebcam", "liveCamWindow", "location=0,status=0,scrollbars=0,width=800,height=535");
	liveCamWindow.moveTo(20, 20);
}

function openMediaplayer(){
	mediaPlayerWindow = window.open("http://195.87.191.240/2010/TiklaDinle_mp.asp", "mediaPlayer", "location=0,status=0,scrollbars=false,width=350,height=310");
	mediaPlayerWindow.moveTo(20, 20);
}










