jQuery.noConflict();

/*
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});
*/

function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    return vars;
}

jQuery(document).ready(function() {
	
	jQuery.each( jQuery("#menu_main li a"), function(i,n){
		if(jQuery(this).attr('href') == '/videotube') {
			jQuery(this).parent().addClass('videotube');
		}
	});
	
	/**
	 * Menu mousover
	 */
	jQuery("#menu_main li").mouseover(function(){
		jQuery(this).addClass("mouseover");
    }).mouseout(function(){
    	jQuery(this).removeClass("mouseover");
    });
	
	/**
	 * Homepage video
	 */
	if(jQuery("#video_left").length) {
		var autostart = getUrlVars()['autostart'];
		var func = function(){
			var obj = this;
			if( obj == null || obj == window ) { // happens when not called as event
				obj = jQuery("#video_left");
			}
			var parameter = (jQuery("body").attr("id")=="videotube") ? 'ugc' : 'ProductID';
			jQuery.ajax({
			   type: "GET",
			   url: "/player/player_mp.php",
			   data: parameter+"="+jQuery(obj).attr("class")+"&rand="+Math.random(),
			   success: function(msg){
					jQuery("#video_left").html(msg);
					jQuery("#video_left").unbind("click");
			   }
			}); 
		};
		
		if( autostart == "1" ) {
			
			func();
		}
		else {
			jQuery("#video_left").click( func );
		}
	}
	
	/**
	 * List item carousel
	 */
	if(jQuery("#list_items").length > 0) {
		jQuery("#list_items").jcarousel({
	        scroll: 1,
	        initCallback: mycarousel_initCallback,
	        itemVisibleInCallback:  highlight,
	    	itemVisibleOutCallback: removehighlight,
	        buttonNextHTML: null,
	        buttonPrevHTML: null
	    });
	}
	if(jQuery("#homepage_carrousel_content").length > 0) {
		jQuery("#homepage_carrousel_content").jcarousel({
	        scroll: 3,
	        initCallback: mycarousel_initCallback,
	        buttonNextHTML: null,
	        buttonPrevHTML: null
	    });
	}
	
	if(jQuery("#content_right_sponsor").length > 0) {
		jQuery("#content_right_sponsor").jcarousel({
	        auto: 5,
	        scroll: 1,
	        wrap: "last"
	    });
	}
	/**
	 * Referer RSS, start the video immediately
	 * 
	 */
	var myFile = document.location.toString();
	if (myFile.match('#')) { 
	  var myAnchor = myFile.split('#')[1];
	  if(myAnchor == "first") {
		  jQuery("#video_left").click();
	  } else {
		  jQuery('#' + myAnchor).click();
	  }
	}
});

/**
 * Pagination callback for video
 */
function mycarousel_initCallback(carousel) {
	jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
        return false;
    });
 
	jQuery('#mycarousel-next').bind('click', function() {
        carousel.next();
        return false;
    });
 
	jQuery('#mycarousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
};

/**
 * Pagination add highlight
 */
function highlight(carousel, obejctli,liindex,listate){
	jQuery('.jcarousel-control li:nth-child('+ (liindex+1) +')').addClass("selected");
};

/**
 * Pagination remove highlight
 */
function removehighlight(carousel, obejctli,liindex,listate){
	jQuery('.jcarousel-control li:nth-child('+ (liindex+1) +')').removeClass("selected");
};

