
$(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 = /*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);
    }

    // rotateButtons({delay: 5});
    // 
    // jQuery.fn.rotateButtons = function() {
    //     var args = arguments[0] || {}; // It's your object of arguments
    //     var dataSource = args.delay;
    // 
    // };


    // // for broadcast menus
    // $('#broadcast .menu li').click(function() {
    // 	$('#broadcast .menu li').removeClass('active');
    // 	$('#broadcast .menu li').find(":first-child").removeClass('active');
    // 	
    // 	$(this).addClass('active');
    // 	$(this).find(":first-child").addClass('active');
    // 
    // });

    // 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" });
    });

    // // for "review now" button
    // if ( getUrlVars()["page"] = "review" ) {
    // 	setPageAndTabActive( "2" );
    // }

    var features = $('#features div.feature div.description');
    fixFeaturesHeights(features);
	var lowerFeatures = $('#context div.lowerFeatures div.lowerFeature p.description');
    fixFeaturesHeights(lowerFeatures);
	
	var productFeatures = $('#context #page1 #features div.feature div.featureTitle');
    fixFeaturesHeights(productFeatures);
	var productFeaturesTitle = $('div.productFamily #context div.feature div.title');
    fixFeaturesHeights(productFeaturesTitle);
	var productFeaturesText = $('div.productFamily #context div.feature div.featureText');
    fixFeaturesHeights(productFeaturesText);

	fixShowCases();
	
    function fixFeaturesHeights(features) {
        var higherFeaturesHeights = 0;
        $(features).each(function(key, featureObj) {
            if (higherFeaturesHeights <= $(featureObj).height()) {
                higherFeaturesHeights = $(featureObj).height();
            }
        });

        $(features).css('height', higherFeaturesHeights);
    }
	
	function fixShowCases() {
		var showcase1 = $('#broadcast #showcase1');
		if ($(showcase1)) {
			var showcase2 = $('#broadcast #showcase2');			
			if ($(showcase2)) {
				$(showcase2).css('top', $(showcase1).attr('offsetTop') + $(showcase1).attr('offsetHeight') + 6);
			
				var showcase3 = $('#broadcast #showcase3');
				if ($(showcase3)) {
					$(showcase3).css('top', $(showcase2).attr('offsetTop') + $(showcase2).attr('offsetHeight') + 6);
				}
			}
		}
	}
});


// 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;
// }


