
$(document).ready(function() {

    //	$(document).pngFix();

    var timer;

    // figure out how many buttons there are
    var numButtons = $('#mainWrapper.index #broadcast .buttonHolder .button').size();

    // get random button
    var currentButton = 1 ; /*Math.floor(Math.random() * numButtons); /* + 1;*/
    // change the page to that button
    changeToPage(currentButton);
    // begin rotation
    autoRotate();

    // change the current page to the given page
    function changeToPage(pageNum) {
        // blank out all
        $("#mainWrapper.index #broadcast .buttonHolder .button").removeClass('highlight');
        $("#mainWrapper.index #broadcast .pages").addClass('noDisplay');

        // hilight the given button
        $("#mainWrapper.index #broadcast .buttonHolder #button" + pageNum + ".button").addClass('highlight');
        // hilight the page associated with that button
        $("#mainWrapper.index #broadcast #page" + pageNum).removeClass('noDisplay');
    };

    // add page changing functionality to buttons
    $('#mainWrapper.index #broadcast .buttonHolder .button').click(function() {
        clearInterval(timer);

        var buttonName = $(this).attr("id");
        var length = buttonName.length;
        var buttonNum = buttonName.charAt(length - 1);

        changeToPage(buttonNum);
    });

    // Changes the pages on an interval
    function autoRotate() {
        var intervalTime = 38000;
        if (numButtons > 1) {
            timer = setInterval(function() {
                // increment current page
                currentButton++;

                // switch the page
                flipToNextPage();
            }, intervalTime);
        };
    };

    function flipToNextPage() {
        if (currentButton > numButtons)
            currentButton = 1;
        changeToPage(currentButton);
    }

    // for tabs
    $('#context #tabHolder .tab').click(function() {

        var a = $(this).attr("name");        
        setPageAndTabActive(a);
    });
    

    // for scrollboxes	
    $("#context .scrollOpenBox .title").toggle(
		function() {
		    $arrow = $(this).children('img');
		    $arrow.css({ "-webkit-transform": 'rotate(0deg)' });
		    $arrow.css({ "-moz-transform": 'rotate(0deg)' });
		    $(this).siblings('.content').css({ "display": "block" });
		},
		function() {
		    $arrow = $(this).children('img');
		    $arrow.css({ "-webkit-transform": 'rotate(-90deg)' });
		    $arrow.css({ "-moz-transform": 'rotate(-90deg)' });
		    $(this).siblings('.content').css({ "display": "none" });
		}
    );

    $('.popupShowcase').click(function() {
        $(this).find(".showcaseMovie").modal({ overlayClose: true });
    });
    $('.reviewPopupButton').click(function() {
        var urlstring = $(this).siblings(".noDisplay").find(".url").html();
        $(this).siblings(".noDisplay").find(".modalPopup").append("<iframe allowTransparency='true' src= '" + urlstring + "' id='frame1' frameborder='0' width='390' height='370' ></iframe>");

        $(this).siblings(".noDisplay").find(".modalPopup").modal({ containerId: "simplemodal-container-nobackground" });
    });

    $('.shopnowPopupButton').click(function() {
        var urlstring = $(this).siblings(".noDisplay").find(".url").html();
        $(this).siblings(".noDisplay").find(".modalPopup").append("<iframe allowTransparency='true' src= '" + urlstring + "' id='frame1' frameborder='0' width='600' height='370' ></iframe>");

        $(this).siblings(".noDisplay").find(".modalPopup").modal({ containerId: "simplemodal-container-nobackground" });
    });

    var features = $('#features div.feature div.description');
    fixFeaturesHeights(features);
	var lowerFeatures = $('#context div.lowerFeatures div.lowerFeature p.description');
    fixFeaturesHeights(lowerFeatures);

    function fixFeaturesHeights(features) {
        var higherFeaturesHeights = 0;
        $(features).each(function(key, featureObj) {
            if (higherFeaturesHeights <= $(featureObj).height()) {
                higherFeaturesHeights = $(featureObj).height();
            }
        });

        $(features).css('height', higherFeaturesHeights);
    }
	
	if ($(".searchedData"))
	{
		$(".searchedData").val("");
		
		$(".searchedData").focus(function () {
			if ($(this).val() == $(this)[0].title) {
				$(this).removeClass("defaultTextActive");
				$(this).val("");
			}
		});

		$(".searchedData").blur(function () {
			if ($(this).val() == "") {
				$(this).addClass("defaultTextActive");
				$(this).val($(this)[0].title);
				isEmptySearch = true;
			}
			else {
				isEmptySearch = false;
			}
		});

		$(".searchedData").blur();
	}


 
    // *** ADDED BY BJ NOV.2.2010
    // for cta's
    $('#features .feature .featureImage img.cta').click(function () {
        $('#context #tabHolder .tab').removeClass('active');
        $('#context .page').removeClass('active');

        var b = $(this).attr("name");
        $('#context #tabHolder .tab[name=' + b + ']').addClass('active');
        $('#context .page[name=' + b + ']').addClass('active');
    });

    // for scrollboxes	
    $("#context .scrollOpenBox .title").toggle(
		function () {
		    $arrow = $(this).children('img');
		    $arrow.css({ "-webkit-transform": 'rotate(0deg)' });
		    $arrow.css({ "-moz-transform": 'rotate(0deg)' });
		    $(this).siblings('.content').css({ "display": "block" });
		},
		function () {
		    $arrow = $(this).children('img');
		    $arrow.css({ "-webkit-transform": 'rotate(-90deg)' });
		    $arrow.css({ "-moz-transform": 'rotate(-90deg)' });
		    $(this).siblings('.content').css({ "display": "none" });
		}
    );
      

   // $('.showcase').click(function () {

   //     $(this).find(".showcaseMovie").modal({ overlayClose: true });

 //   });


});


// rating reviews
function changeButtonSetToGrey(obj) {
	var button = $(obj).parent();
	
	button.addClass('greyButton');
	button.siblings('.blueButton').addClass('greyButton');
};


function changeParentURL( url ) {
	document.location = url;
}


function closeModal() {
	// must be called first	
	$.modal.close();

	// remove old popups
	$(".modalPopup").empty();

}


function setPageAndTabActive( pagenum ) {
	$('#context #tabHolder .tab').removeClass('active');
	$('#context .page').removeClass('active');
	
	$('#context #tabHolder .tab[name=' + pagenum + ']').addClass('active');
	$('#context .page[name=' + pagenum + ']').addClass('active');
}


// 
// // Read a page's GET URL variables and return them as an associative array.
// 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;
// }

//slider fix for ie9
(function($){var a=$.ui.mouse.prototype._mouseMove;$.ui.mouse.prototype._mouseMove=function(b){if($.browser.msie&&document.documentMode>=9){b.button=1};a.apply(this,[b]);}}(jQuery));
