var img = new Image();
var page = 1;
var ySound = false;

var pageNews = 1;

jQuery(document).ready(function() {

	//jQuery('.news ul li:last').css('height', '20px');
	//jQuery('.win_videos .vs ul li:first').css('margin-left', '0px');
	
	jQuery('.closer').click(
		function() {
			layhide();
		}
	);
	
	jQuery('.vs ul li img').click(
		
		function() {
			
			loadVideo(jQuery(this).attr('alt'));
		}
		
	);
	
	jQuery(".playmp3").click(
		function() {
			playmp3();
		}
	);
	
	
	if (jQuery('#scrollbar_track_news').length > 0) {
		var news_scroll = new Control.ScrollBar('scrollbar_content_news', 'scrollbar_track_news');
		jQuery("#scrollbar_track_news").show();
	}
	
	
	if (jQuery('#scrollbar_track_music').length > 0) {
		var music_scroll = new Control.ScrollBar('scrollbar_content_music', 'scrollbar_track_music');
		jQuery("#scrollbar_track_music").show();
		
		jQuery('#scrollbar_content_music li img').click(
			function() {
				
				load_mp3(jQuery(this).attr('class'));
			}
		);
	}
	
	jQuery(".newer").click(
		function() {
			pageNews = --pageNews;
			pageNews = pageNews <= 1 ? 1 : pageNews;
			var url = ulang + '/news/page/' + pageNews;
			jQuery("#content_news .news ul").html("");
			jQuery.get(url, function(data) {
				jQuery("#content_news .news ul").html(data);
				jQuery("#content_news .news ul li a").click(
					
					function() {
						
						id = jQuery(this).attr('id');
						id = id.substring(5);
						jQuery('.article').html("");
						jQuery.get(ulang + '/news/get/'+id,
							function(data) {
								jQuery(".article").html(data);
								var news_scroll = new Control.ScrollBar('scrollbar_content_news', 'scrollbar_track_news');
							}
						);
					}
				);

			});
			
		}
		
	);
	
	jQuery(".older").click(
		function() {
			pageNews = ++pageNews;
			pageNews = pageNews >= maxpage ? maxpage : pageNews;
			var url = ulang + '/news/page/' + pageNews;
			jQuery("#content_news .news ul").html("");
			jQuery.get(url, function(data) {
				jQuery("#content_news .news ul").html(data);
				jQuery("#content_news .news ul li a").click(
					
					function() {
						id = jQuery(this).attr('id');
						id = id.substring(5);
						jQuery('.article').html("");
						jQuery.get(ulang + '/news/get/'+id,
							function(data) {
								jQuery(".article").html(data);
								var news_scroll = new Control.ScrollBar('scrollbar_content_news', 'scrollbar_track_news');
							}
						);
					}
				);
			});	
			
		}
	);
	
	jQuery("#content_news .news ul li a").click(
		
		function() {
			
			id = jQuery(this).attr('id');
			id = id.substring(5);
			jQuery('.article').html("");
			jQuery.get(ulang + '/news/get/'+id,
				function(data) {
					jQuery(".article").html(data);
					var news_scroll = new Control.ScrollBar('scrollbar_content_news', 'scrollbar_track_news');
					jQuery("#scrollbar_track_news").show();
				}
			);
		}
	);
	
	jQuery("#email").focus(
		
		function() {
			
			if (jQuery('#email').val() == 'Please Enter Email Address') {
				jQuery('#email').val('');
			}
			return false;
		}
	);
	
	jQuery("#email").blur(
		
		function() {
			
			if (jQuery("#email").val() == '') {
				jQuery('#email').val('Please Enter Email Address');
			}
			
			return false;
		}
	)
	
	
});


function divhide() {
	
	jQuery('#overlay .win_about').hide('fast');	
	jQuery('#overlay .win_photos').hide('fast');	
	jQuery('#overlay .win_videos').hide('fast');	
	jQuery('#overlay .win_store').hide('fast');	

}

function layshow(win) {
	
	if (ySound!= false) {
		ySound.stop();
	}
	
	jQuery('#overlay').css('height', jQuery(document).height() + 'px')

	jQuery('#overlay').show('slow');
	divhide();
	jQuery('#overlay .win_'+win).slideDown('normal');

	if (win == 'about') {
		
		var bio_scroll = new Control.ScrollBar('scrollbar_content_bio', 'scrollbar_track_bio');
	}
	else if(win == 'photos') {

		var apiKey = "2cd34777de8b3efa688e8dc4f570c639";
		var photoSetID = "72157621887763297";
		var totalPages = 1;
		
		if (jQuery('#yphotos img').length <= 0) {
			jQuery.getJSON('http://api.flickr.com/services/rest/?&per_page=1&page=' + page + '&method=flickr.photosets.getPhotos&api_key=' + apiKey + 	'&photoset_id=' + photoSetID + '&format=json&jsoncallback=?',
                function(data){
                    
						  totalPages = data.photoset.total;
                    //loop through the results with the following function
                    jQuery.each(data.photoset.photo, function(i,item){
                    
                        //build the url of the photo in order to link to it
                        var photoURL = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '.jpg';
								jQuery('#yphotos').html('<img src="' + photoURL + '" width="500"  />');

								//img.onLoad = loadImage(photoURL);
								//img.src = photoURL;
						  });
					}
			);
		}
		
		jQuery('.win_photos .next').mousedown(
			function() {
				
			
				if (page == totalPages) {
					
					page = 1;
				}
				else {
					page++;
				}

				jQuery('#yphotos img').fadeOut('slow');			

				jQuery.getJSON('http://api.flickr.com/services/rest/?&per_page=1&page=' + page + '&method=flickr.photosets.getPhotos&api_key=' + apiKey + 	'&photoset_id=' + photoSetID + '&format=json&jsoncallback=?',
							 function(data){
								  
								  totalPages = data.photoset.total;
								  //loop through the results with the following function
								  jQuery.each(data.photoset.photo, function(i,item){
								  
										//build the url of the photo in order to link to it
										var photoURL = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '.jpg';
										jQuery('#yphotos').html('<img src="' + photoURL + '" width="500"  />');
										
								  });
							}
				);
				
				
			}
		);

		jQuery('.win_photos .prev').mousedown(
			function() {
				
				jQuery('#yphotos img').fadeOut('slow');			
				--page;
				if (page <= 0) {
					
					page = totalPages;
				}

				jQuery.getJSON('http://api.flickr.com/services/rest/?&per_page=1&page=' + page + '&method=flickr.photosets.getPhotos&api_key=' + apiKey + 	'&photoset_id=' + photoSetID + '&format=json&jsoncallback=?',
							 function(data){
								  
								  totalPages = data.photoset.total;
								  //loop through the results with the following function
								  jQuery.each(data.photoset.photo, function(i,item){
								  
										//build the url of the photo in order to link to it
										var photoURL = 'http://farm' + item.farm + '.static.flickr.com/' + item.server + '/' + item.id + '_' + item.secret + '.jpg';
										jQuery('#yphotos').html('<img src="' + photoURL + '" width="500" />');

								  });
							}
				);
				
				
			}
		);		
		
		
	}
	else if(win == 'videos') {

		jQuery(".vs").jCarouselLite({
			btnNext: ".win_videos .next",
			btnPrev: ".win_videos .prev",
			visible: 4,
			circular: true
		});
		
		loadPlayer(jQuery('.vs ul:first-child img').attr('alt'));
	}
}


function loadImage(photoURL) {
	//jQuery('#yphotos').hide('fast', function() {
		jQuery('#yphotos').html('<img src="' + photoURL + ' width="500" height="375" />');
		//jQuery('#yphotos img').slideDown('slow', function() { jQuery(this).center();	});
		//jQuery('#yphotos').fadeIn('slow');
//	});
	//jQuery('#yphotos img').center();
}
function layhide(win) {
	
	divhide();
	jQuery('#overlay').hide('fast');
	
	if (win == 'photos') {
		
		jQuery('#yphotos').html("");
	}
}



function playmp3() {
	
}

var prevTrack = '';

function load_mp3(id) {

	var url = '/music/get/' + id;

	jQuery('#scrollbar_content_music li img').attr({
		src: '/images/Button_Play.png'
	});

	
	if (ySound) {
		ySound.destruct();
	}


	if (prevTrack == id) {
		prevTrack = '';
		return false;
	}

	jQuery('.' + id).attr({
		src: '/images/Button_Stop.png'
	});
	
	jQuery.get(url, function(data) {

				
		ySound = soundManager.createSound({
	   	id: 'aSound',
			 url: data
		});

		ySound.play();
		
	});	

	prevTrack = id;
	return false;
}

function subscribe() {

	var post_url = jQuery('#navs form').attr('action');
	jQuery.post(post_url, {email: jQuery('#email').attr('value')},
		function(data) {
			alert(data);					
		}
	);
}