
/**
*  GLOBALS VARIABLES
*  @todo need to be defined  where they are used...
**/

 var site_list_open = false;
 var timer = null;

/**
*  Mysterious function not used anywhere...
*  ------------------------------------------------
*  @param
*  @return
*  @todo    to be remove
**/
function gup(name)
{
    //name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    //var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regexS = "([0-9]+)";
    var regex = new RegExp( regexS );
    var results = regex.exec( window.location.href );
    ///alert(results);
    if( results == null )
        return "";
    else
        return results[1];
}


/**
*  Tag something as a favorites for this user.
*  ------------------------------------------------

*  @param   type        model, scene, video, picture
*  @param   id          the id of the item
*  @param   scene_id    the id of the scene related to that item
*  @call    ajax
*  @return  void
*  @todo    clean
**/
var favedScenes = {};
function addFavorite(type, id, scene_id, nopopup) {
//    alert("id in: " + id + ", scene_id:" + scene_id);

	if (typeof scene_id == 'boolean') {
		nopopup = scene_id;
		scene_id = null;
	}

	// Prevent many requests of the same ID
	/*if (!favedScenes[''+type+id+scene_id])
		favedScenes[''+type+id+scene_id] = true;
	else
		return false;*/

    var url = "?guimode=ajax&action=addfavorite&type=" + type + "&id=" + id + "&scene_id=" + scene_id;
    $.get(url, function(data) {
    	if (nopopup) {} else {processReturn(data);}
    });
    return false;
}


/**
*  Tag something as a favorites for this user.
*  ------------------------------------------------

*  @param   data        a string messages receive from a callback
*  @return  void
*  @todo    clean
**/
function processReturn(data) {
    $("#ajax_messages").html(data);
    $('#ajax_messages').slideDown('slow');

    setTimeout ( "$('#ajax_messages').slideUp('slow')", 5000 );
}


/**
*  Function call before a search submit to prepare the search
*  ------------------------------------------------
*  @return  void
*  @todo    clean
**/
function doSearch()
{
    var term = $('#header_search_term_fake').val();
    var filter_form = $('#search-filters');
    if (filter_form.length > 0)
    {
        $('#search_term').val(term);
        $('#search-filters').submit();
    }
    else
    {
        $('#header_search_term_real').val(term);
        $('#header-search_real').submit();
    }
}

function doSearchFromProfile()
{
    var term = $('#header_search_term_fake').val();
    var filter_form = $('#search-filters');
    if (filter_form.length > 0)
    {
        $('#search_term').val(term);
        $('#search-filters').submit();
    }
    else
    {
        $('#header_search_term_real').val(term);
        $('#header-search_real').submit();
    }
}

site_list_open = false;



/**
*  Open/Close the list of the site in the left nav
*  ------------------------------------------------
*  @return  void
*  @todo   clean
**/
function toggleSiteList() {
    $("#site_list").removeClass("leftnav-bullets");

    if (site_list_open) {
        $('#site_list').slideUp('slow');
        site_list_open = false;
    }
    else {
        $('#site_list').slideDown('slow');
        site_list_open = true;
    }
    $("#site_list").addClass("leftnav-bullets");
}


/**
*  Call a Flash Player in a User Profile
*  ------------------------------------------------

*  @param   part        A speficic part of the scene ?
*  @param   scene_id    id of the scene
*  @param   type        type of video
*  @param   ext         video format ?
*  @param   video       source of the file
*  @param   is_trial    does the user is a trial ?
*  @param   autoplay    autoplay switch
*  @param   squished    switch that change the size of the player.
*  @return  void
*  @todo    clean and maybe merge with other function
**/
function showPlayerFromProfile(part, scene_id, type, ext, video, is_trial, autoplay, squished)
{
    if (typeof(autoplay) == "undefined") {
        autoplay = 0;
    }
    if (typeof(squished) == "undefined") {
        squished = 0;
    }
    if (squished) {
        var width = 503;
        var height = 397;
        var fs = 'true';
    }
    else {
        var width = 640;
        var height = 506;
        var fs = 'true';
    }

    if(is_trial)
    {
        width = 400;
        height = 326;
        fs = 'false';
    }
    var targetID = "#video_wrapper";
    if (is_trial) {
        targetID = "#porn_trial"
    }
    var browserversion = navigator.appVersion;
    var is_mac = ((browserversion.toLowerCase().indexOf("mac") != -1) ? true : false);
    if (ext == 'wmv')
    {
        height += 50;
        if (is_mac)
        {
            $(targetID).html('<embed type="application/x-mplayer2" src="'+video+'" name="MediaPlayer width="'+ width +'" height="'+ height +'" showStatusBar="true" scale="ASPECT"></embed>');
        }
        else
        {
            if (window.ie7)
            {
                $(targetID).html('<object classid="CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6" type="application/x-oleobject" width="'+ width +'" height="'+ height +'" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715" standby="Loading Microsoft Windows Media Player components..."><param name="url" value="'+video+'"><param name="autostart" value="' + autoplay +'"><param name="ShowStatusBar" value="true"><param name="volume" value="100"><param name="stretchToFit" value="false"></object>');
            }
            else
            {
                if(is_trial) target = 'porn_trial';
                    else target = 'video_wrapper';

                var player = document.getElementById(target);
                player.innerHTML = '<object id="mplayer" width="'+ width +'" height="'+ height +'" type="application/x-mplayer2"><param name="fileName" value="'+video+'"><param name="autostart" value="' + autoplay +'"><param name="ShowStatusBar" value="1"><param name="volume" value="0"></object>';
                //$(targetID).html('<object id="mplayer" width="'+ width +'" height="'+ height +'" type="application/x-mplayer2"><param name="fileName" value="'+video+'"><param name="autostart" value="' + autoplay +'"><param name="ShowStatusBar" value="1"><param name="volume" value="0"></object>');
                //$(target).setHTML('<object id="mplayer" width="'+ width +'" height="'+ height +'" type="application/x-mplayer2"><param name="fileName" value="'+video+'"><param name="autostart" value="' + autoplay +'"><param name="ShowStatusBar" value="1"><param name="volume" value="0"></object>');
            }
        }
        $(targetID).show();
    }
    else
    {
        if(type == 'Parts') {
            file = 'parts.php';
            url = 'videooptions/' + file + '?infos=' + scene_id + '/' + (part-1) + '/' + ext + '/' + autoplay;
        }
        else if(type =='OneMin') {
            file = 'one_min.php';
            url = 'videooptions/' + file + '?infos=' + scene_id + '/' + (part-1) + '/' + ext + '/' + autoplay;
        }
        else if(type == 'Trailer') {
            file = 'trailer.php';
            url = 'videooptions/' + file + '?infos=' + scene_id + '//' + ext + "/" +autoplay ;
                }
        else {
            file = '';
            url = 'videooptions?infos=' + scene_id + '//' + ext + '/' + autoplay;
        }
        var target = 'porn_' + scene_id;
        html =  '<div id="porn_' + scene_id + '"></div>'+
                '<script language="javascript">' +
                //'var s1 = new SWFObject("player/brazzers_new_player2.swf?v=2", "pornplayer", "' + width + '", "' + height + '", "9.0.115.0");' +
                'var s1 = new SWFObject("http://static.brazzers.com/player/brazzers_new_player2.swf", "pornplayer", "' + width + '", "' + height + '", "9.0.115.0");' +
                's1.addParam("allowFullScreen","' + fs + '");' +
                's1.addParam("AllowScriptAccess","always");' +
                's1.addParam("wmode", "transparent");' +
                's1.addVariable("options", "' + url + '");' +
                's1.write("porn_' + scene_id + '");' +
                '</script>';
//        $(document).ready(function(e) {
            $(targetID).html(html);
            $(targetID).show();
//        });
    }

    // now set the currently Currently Loaded Video span id=current_video if type=MainVideo or Parts
    if(type == "MainVideo")
        $("#current_video").html("Full Video");
    if(type == "Parts")
        $("#current_video").html("Partial Video #" + part);
    if(type == "OneMin")
        $("#current_video").html("One Minute Clip #" + part);
}

function showPlayer(part, scene_id, type, ext, video, is_trial, autoplay, squished)
{
	if ((typeof BSPlayer == 'object' || typeof BSPlayer == 'function') && (typeof pornoman == 'object' || typeof pornoman == 'function'))
	{
		return false;
	}

	if (typeof(autoplay) == "undefined") autoplay = 0;
	if (typeof(squished) == "undefined") squished = 0;

	if (ext == 'wmv' || ext == 'flv')
	{

		if (squished)
		{
			var width = 503;
			var height = 397;
			var fs = 'true';
		}
		else
		{
			var width = 640;
			var height = 506;
			var fs = 'true';
		}

		if(is_trial)
		{
			width = 400;
			height = 326;
			fs = 'false';
		}

		var targetID = "#video_wrapper";

		if (is_trial) {
			targetID = "#porn_trial"
		}

		var browserversion = navigator.appVersion;
		var is_mac = ((browserversion.toLowerCase().indexOf("mac") != -1) ? true : false);

		if (ext == 'wmv')
		{
			height += 50;
			if (is_mac)
			{
				$(targetID).html('<embed type="application/x-mplayer2" src="'+video+'" name="MediaPlayer width="'+ width +'" height="'+ height +'" showStatusBar="true" scale="ASPECT"></embed>');
			}
			else
			{
				if (window.ie7)
				{
					$(targetID).html('<object classid="CLSID:6BF52A52-394A-11D3-B153-00C04F79FAA6" type="application/x-oleobject" width="'+ width +'" height="'+ height +'" codebase="http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=6,4,5,715" standby="Loading Microsoft Windows Media Player components..."><param name="url" value="'+video+'"><param name="autostart" value="' + autoplay +'"><param name="ShowStatusBar" value="true"><param name="volume" value="100"><param name="stretchToFit" value="false"></object>');
				}
				else
				{
					if(is_trial) target = 'porn_trial';
						else target = 'video_wrapper';

					var player = document.getElementById(target);
					player.innerHTML = '<object id="mplayer" width="'+ width +'" height="'+ height +'" type="application/x-mplayer2"><param name="fileName" value="'+video+'"><param name="autostart" value="' + autoplay +'"><param name="ShowStatusBar" value="1"><param name="volume" value="0"></object>';
					//$(targetID).html('<object id="mplayer" width="'+ width +'" height="'+ height +'" type="application/x-mplayer2"><param name="fileName" value="'+video+'"><param name="autostart" value="' + autoplay +'"><param name="ShowStatusBar" value="1"><param name="volume" value="0"></object>');
					//$(target).setHTML('<object id="mplayer" width="'+ width +'" height="'+ height +'" type="application/x-mplayer2"><param name="fileName" value="'+video+'"><param name="autostart" value="' + autoplay +'"><param name="ShowStatusBar" value="1"><param name="volume" value="0"></object>');
				}
			}
			$(targetID).show();
			if (autoplay)
			{
				$.scrollTo(targetID, 'slow');
			}
		}
		else
		{

			if(type == 'Parts') {
				file = 'parts.php';
				url = 'videooptions/' + file + '?infos=' + scene_id + '/' + (part-1) + '/' + ext + '/' + autoplay;
			}
			else if(type =='OneMin') {
				file = 'one_min.php';
				url = 'videooptions/' + file + '?infos=' + scene_id + '/' + (part-1) + '/' + ext + '/' + autoplay;
			}
			else if(type == 'Trailer') {
				file = 'trailer.php';
				url = 'videooptions/' + file + '?infos=' + scene_id + '//' + ext + "/" +autoplay ;
			}
			else {
				file = '';
				url = 'videooptions?infos=' + scene_id + '//' + ext + '/' + autoplay;
			}



			if ($(".trial_popup_block").size() == 1)
			{
				var pversion = 'brazzers_new_player10';
			}
			else
			{
				var pversion = 'brazzers_new_player2';
			}

			var target = 'porn_' + scene_id;
			html =  '<div id="porn_' + scene_id + '"></div>'+
					'<script language="javascript">' +
					//'var s1 = new SWFObject("player/brazzers_new_player2.swf?v=2", "pornplayer", "' + width + '", "' + height + '", "9.0.115.0");' +
					'var s1 = new SWFObject("http://static.brazzers.com/player/' + pversion + '.swf", "pornplayer", "' + width + '", "' + height + '", "9.0.115.0");' +
					's1.addParam("allowFullScreen","' + fs + '");' +
					's1.addParam("AllowScriptAccess","always");' +
					's1.addParam("wmode", "transparent");' +
					's1.addVariable("options", "' + url + '");' +
					's1.write("porn_' + scene_id + '");' +
					'</script>';


			$(targetID).html(html);
			$(targetID).show();
			if (autoplay)
			{
				$.scrollTo(targetID, 'slow');
			}
		}
	}
	else
	{

		var is_hd = $("#is_hd").val();
		var is_wide = $("#wide_player").val();

		var aspect_ratio = $("#aspect_ratio").val();
		var showinfo = 1;
		if(autoplay) showinfo = 0;

		if(aspect_ratio == '16:9'){
			var width = 768;
			var height = 432 + 6;
		}else{
			var width = 768;
			var height = 480 + 6;
		}


		switch(type)
		{
			case "Parts":
				var part = part -1;
				break;
			case "OneMin":
				var part = part -1;
				break;
			case "MainVideo":
				var part = 0;
				break;
			case "hdvideo_768":
				var part = 0;
				break;
			case "hdvideo_1280":
				var part = 0;
				var width = 975;
				var height = 548 + 6;
				break;
			case "trailer":
				var part = 0;
				break;
		}


		var videooptions_url = "get_scenes/" + scene_id + "/"+ type + "/" + part + "/" + ext + "/autoplay-" + autoplay + "/show-info-" + showinfo;


		try {
			$('#fl_stream').val(videooptions_url);
		} catch (e) {}

		var player = $("#porn");


		var flashvars = {
			options: videooptions_url,
			autostart: 'true',themeColor: '0395d3',mode: 'overlay',scaleMode: 'stretch',frameColor: '333333',fontColor: 'cccccc',link: '',embed: ''
		};

		var params = {allowFullScreen: 'true', 'wmode': 'transparent', allowscriptaccess: 'always'};
		var attributes = {id: 'porn',name: 'porn', wmode:'transparent'};
		swfobject.embedSWF('http://static.brazzers.com/player/wide_ma_player.swf', 'porn', width, height, '10.0.0', 'expressInstall.swf', flashvars, params, attributes);


		var scene_title = $(".scene_navigation");
		var page_url 	= window.location.href;

		if(page_url.indexOf('#comments') == -1)
		{
			$.scrollTo(scene_title, 'slow');
		}


		var site_shortname = $("#site_shortname").val();

			if(typeof(pageTracker) != "undefined"){
			pageTracker._trackEvent('Stream&Download', 'Stream-' + type ,  site_shortname + "/" + scene_id, 1);
			}
	}

}



/**
*  Call a Flash Player in the Home Header
*  ------------------------------------------------
* todo: make it dynamic depending of the extension requested...
**/
function showHeaderPlayer(scene_id, ext)
{
      var videooptions_url = "get_scenes/" + scene_id + "/HeaderTrailer/0/" + ext + "/autoplay-0/show-info-0";

      var player = $("#porn");

      player.html("");
      player.flash(
      {
            src: "http://static.brazzers.com/player/wide_ma_player.swf",
            width: 351,
            height: 229,
            wmode: "transparent",
            align: "middlel",
            allowfullscreen:true,
            allowscriptaccess: "always",
            allownetworkaccess: "always",
            flashvars: {
                options: videooptions_url
            }
       },
       {version:"10.0.22"}
       );
}


/**
 *
 * Clicks on the 480p or 720p tabs
 * (this function is called from the player)
 *
 **/
 function toggleHdTabs(type)
 {
 	if(type=='hdvideo_1280')
 	{
		$("#720p_link").click();
 	}
 	else
 	{
		$("#480p_link").click();
 	}
 }


/**
*  Call a Flash Player in a User Profile
*  ------------------------------------------------
*  @todo  clean and maybe merge with other top function
**/

function showInterviewPlayer(id, vurl)
{
    var scene_id = '';

    var width = 640;
    var height = 506;
    var fs = 'true';
    var file = 'simple.php';

    var url = 'videooptions/' + file + '?url=' + escape(vurl);
    var html =  '<div id="porn_' + id + '"></div>'+
            '<script language="javascript">' +
            'var s1 = new SWFObject("http://static.brazzers.com/player/brazzers_new_player2.swf", "pornplayer", "' + width + '", "' + height + '", "9.0.115.0");' +
            's1.addParam("allowFullScreen","' + fs + '");' +
            's1.addParam("AllowScriptAccess","always");' +
            's1.addParam("wmode", "transparent");' +
            's1.addVariable("options", "' + url + '");' +

            's1.write("porn_' + id + '");' +
            '</script>';

    $('#video_wrapper').html(html);
    $('#video_wrapper').show();

    var targetID = '#porn_' + id;
    $.scrollTo(targetID, 'slow');
}



/**
*  Call a Flash Player in a User Profile
*  ------------------------------------------------
*  @todo    clean and maybe merge with other top function
**/

function showInterviewPlayerForProfile(id, vurl)
{
    var scene_id = '';

    var width = 503;
    var height = 397;
    var fs = 'true';
    var file = 'simple.php';

    var url = 'videooptions/' + file + '?url=' + escape(vurl);
    var html =  '<div id="porn_' + id + '"></div>'+
            '<script language="javascript">' +
            'var s1 = new SWFObject("http://static.brazzers.com/player/brazzers_new_player2.swf", "pornplayer", "' + width + '", "' + height + '", "9.0.115.0");' +
            's1.addParam("allowFullScreen","' + fs + '");' +
            's1.addParam("AllowScriptAccess","always");' +
            's1.addParam("wmode", "transparent");' +
            's1.addVariable("options", "' + url + '");' +
            's1.write("porn_' + id + '");' +
            '</script>';

    $('#video_wrapper').html(html);
    $('#video_wrapper').show();

    var targetID = '#porn_' + id;
    $.scrollTo(targetID, 'slow');
}


/**
*  Call a Flash Player in a User Profile
*  ------------------------------------------------
*  @param   id          the id of the videos
*  @param   autoplay    autoplay switch
*  @param   squished    switch that change the size of the player.
*  @return  void
*  @todo    clean and maybe merge with other function
**/
function showUserVidPlayer(id, autoplay, squished)
{
    if (typeof(squished) == "undefined")
    {
        squished = 0;
    }
    if (squished)
    {
        var width = 503;
        var height = 397;
        var fs = 'true';
    }
    else
    {
        var width = 640;
        var height = 506;
        var fs = 'true';
    }

    var file = 'user_vids.php';

    var url = 'videooptions/' + file + '?infos=' + id + '/' + autoplay;
    var html =  '<div id="porn_' + id + '"></div>'+
            '<script language="javascript">' +
            'var s1 = new SWFObject("http://static.brazzers.com/player/brazzers_new_player2.swf", "pornplayer", "' + width + '", "' + height + '", "9.0.115.0");' +
            's1.addParam("allowFullScreen","true");' +
            's1.addParam("AllowScriptAccess","always");' +
            's1.addParam("wmode", "transparent");' +
            's1.addVariable("options", "' + url + '");' +

            's1.write("porn_' + id + '");' +
            '</script>';

    $('#video_wrapper').html(html);
    $('#video_wrapper').show();

    var target = 'porn_' + id;
}


timer = null;


$(document).ready(function()
{
   $("label.overlabel").overlabel();


   $(".pbtn_community, .pbtn_community").click(function()
   {
          $(".community_popup").show();
         return false;
   });

   $(".scene_unlock_720p_show, .scene_unlock_720p_show").click(function()
   {
          $(".scene_unlock_720p_popup").show();
         return false;
   });

   $(".scene_unlock_1080p_show, .scene_unlock_1080p_show").click(function()
   {
          $(".scene_unlock_1080p_popup").show();
         return false;
   });

   $(".pbtn_bzr, .btn_pb_bzr").click(function()
   {
         $.get("index.php?action=ajax/get_member_status", {site:'bzr'}, function(res){
            var info = $.trim(res).split("|");
            if(info[0] == 'full' || info[0] == 'trial')
            {
                $("#bzr_username").val(info[1]);
                $("#bzr_password").val(info[2]);
                $(".bzreel_popup form").submit();
            }
            else
            {

                window.location = $("#bzr_1clicklink").val();
            }
         });

        // $(".bzr_popup").show();
         return false;
   });

   $("a.close").click(function()
   {
        $(this).parents("#popup_container").hide();
         return false;
   });


   /*
     Submit cross login to mofos with captcha overwrite
   */
   $("#get_capcha_and_submit").click(function()
   {
        var form = $(this).parents('form');

        $.get("?action=portal&guimode=ajax&get_capcha=true", function(capcha){
          $("#captcha_bypass").val(capcha);
          form.submit();
        });
   });

    $("#setting_selector div div").hover(function(){$(this).addClass("settings_toggle_hover");}, function(){$(this).removeClass("settings_toggle_hover");});
    $('#settings_toggle').mouseover(function()
    {
        $('#setting_selector').slideToggle();
     });

    $('#setting_selector').mouseout(function()
    {
        timer = setTimeout('closeSettingSelector()', 3000);
    });
    $('#setting_selector').mouseover(function()
    {
        //cancel the closing timer
        clearTimeout(timer);
    });


	/*
	  Rewrite upsell links into nice looking ones..
	*/
	$('a.pbtn_empty, a.pbtn_footer_upsell, .upsell_leftnav a, .partner_block a, partner_block_large a, .portal_box a').each(function() {

		// In case the span with the rewrite url is not present, we dont want to execute this script
		if($(this).attr('name') != '')
		{

			function clickLink() {
				var el = $(this);
				$.data(this, 'href', el.attr('href'));
				el.attr('href', $(this).attr('name'));
			}

			$(this)
			.hover(function() {
				this.href = $(this).attr('name');
			})
			.mouseup(function() {
				this.href = $.data(this, 'href');
			})
			$.proxy(clickLink, this)();
		}
	});


    //Logout button tracking
    $(".btn_logout").click(function(){
		if(typeof(pageTracker) != "undefined"){
       	  pageTracker._trackEvent('GlobalEvent', 'Memberareas' , 'logout' , 1);
	   }
    });


   /**
	*  Clear the default initial value on click
	*  Simply put the default value in the label tag
	*  ------------------------------------------------
	* @author: Mike (added here by GL)
	**/
	$(".clear_input").focus(function(){
		if($(this).attr("label") == $(this).val())
		{
			$(this).val("");
		}
	}).blur(function(){
		if($(this).val() == '')
		{
			$(this).val($(this).attr("label"));
		}
	});
});

function closeSettingSelector(elementId)
{
    $('#setting_selector').slideUp();
}

function showNetworkScenes()
{
    $('#brazzers_content_scenes').fadeOut('fast', function()
    {
        $('#brazzers_network_scenes').fadeIn('fast');
    });
}

function showContentScenes()
{
    $('#brazzers_network_scenes').fadeOut('fast', function()
    {
        $('#brazzers_content_scenes').fadeIn('fast');
    });
}


function getScroll(){
	var scrollY = 0;

	if( document.documentElement && document.documentElement.scrollTop ) {
	    scrollY = document.documentElement.scrollTop;
	}
	else if( document.body && document.body.scrollTop ) {
	    scrollY = document.body.scrollTop;
	}
	else if( window.pageYOffset ) {
	    scrollY = window.pageYOffset;
	}
	else if( window.scrollY ) {
	    scrollY = window.scrollY;
	}

	return scrollY;
}






/**
 * @function: isScrolledIntoView
 * @description: Returns true if a given element (in jQuery) is visible in the window's current scroll range.
 */
function isScrolledIntoView(elem) {
	var docViewTop = $(window).scrollTop();
	var docViewBottom = docViewTop + $(window).height();

	var elemTop = $(elem).offset().top;
	var elemBottom = elemTop + $(elem).height();

	return ((elemBottom >= docViewTop) && (elemTop <= docViewBottom));
}

/**
 * @function: roundFloat
 * @description: Rounds num to dec decimal places and returns it.
 */
function roundFloat(num, dec) {
	if (typeof dec != 'number')
		dec = 2;

	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}


function AddClickTaleTag(Tag)
{
   if (window.ClickTaleTag)
      ClickTaleTag(Tag);
   else
   	  return true;
}