
var newsOpen = false;
var featuresOpen = false;
var openImage = 577;
var closedImage = 354;
var animation = false
var openSpeed = 500;
var closeSpeed = 300;
var currentImage = 0;
var slideSHowSpeed = 5000;
var pageType = "";

var browserSet = false;
var ie78 = false;
var ie8 = false;
var oldfirefox = false;


$(document).ready(function () {
    //Handler for .ready() called.
    //alert("engine")

    //    if (pageType == "fullScreen") {
    //        $.getScript("/common/reportage/js/fullScreenGallery.js", function () {
    //            //alert("loaded");
    //           // buildThumbs();
    //        });
    //    }



    if (browserSet == false) {

        var ua = $.browser;
      

        if (ua.msie) {

            if ((ua.version == 6.0) || (ua.version == 7.0)) {
                ie78 = true
            } else if (ua.version == 8.0) {
                ie8 = true
            }

        } else if (ua.mozilla) {
            if (ua.version.startsWith("1.9.0.")) {
                oldfirefox = true
            } else if (ua.version.substr(0, 3) == '1.8') {
                oldfirefox = true
            }

        } else if ((ua.safari) && (navigator.appVersion.indexOf("Chrome") == -1)) {
            _ver = parseInt(ua.version.substr(0, 3))

            //alert(ua.version);

            if (_ver <= 531) {
                oldfirefox = true
            }
        }


        browserSet = true;
    }

    if (oldfirefox) {
        $(".topnavbigtop").addClass("firefoxbelow");
        $(".topnavsmalltop").addClass("firefoxsmall");
        $(".repphotographers").addClass("repphotographersff");
        $("h1").addClass("firefoxhone");
        $(".featurebox").addClass("firebox");
        $(".multimediabox").addClass("firebox");
    }


    //alert("ie78 " + oldfirefox + " ie78" + ie78)


    setupPage();


    $(document).bind("contextmenu", function (e) {
        return false;
    });

    $(window).resize(function () {
        // alert("engine resize");
        setupPage();
    });
});



function setupPage() {
    var w = $(window).width();
    var h = $(window).height();


    if ((pageType == "represented") || (pageType == "emerging") || (pageType == "featured")) {


        $('.footer').css("position", "fixed");
        $('.footer').css("bottom", "0px");
        $('.footer').css("width", (w - 40) + "px");

        $('.featurecontainer').width(w - 290);

        openImage = h - 320;
        closedImage = openImage - 210;

        if (featuresOpen == true) {
            $('.mainimagearea').height(closedImage);
            $('.righthandrows .leftfade').height(closedImage + 60);
            if ($('body').hasClass('ipad')) {
                $('.righthandrows #newsItems').height((parseInt((closedImage) / 148) * 148));
            } else {
                $('.righthandrows #newsItems').height((parseInt((closedImage) / 218) * 218));
            }
            
        } else {
            $('.mainimagearea').height(openImage);
            $('.righthandrows .leftfade').height(openImage + 60);
            
            if ($('body').hasClass('ipad')) {
                $('.righthandrows #newsItems').height((parseInt((openImage) / 148) * 148));
            } else {
                $('.righthandrows #newsItems').height((parseInt((openImage) / 218) * 218));
            }
        }

        if (pageType == "featured") {
            $('.leftfadelisthw').width(910)
        } else {
            photoNames = (w - 40 - 47 - 210)
            if (photoNames > 675) {
                if (photoNames > 820) {
                    photoNames = 820;
                }
                $('.leftfadelisthw').width(photoNames)
            }
        }


    } else if (pageType == "photographer") {

        $('.footer').css("position", "fixed");
        $('.footer').css("bottom", "0px");
        $('.footer').css("width", (w - 40) + "px");

        $('#multimediaItems').width(w - 215);
        $('#featureItems').width(w - 215);
        $("#departmentcontainer").height(h - 170);

              

		$(".cornerarea").height(h-170)
		$(".department").height(h-170)
		$('.biographybox').height(h - 230)
		$('#chronologyContainer').height(h - 200)	

	

		$(currentActive).width(w - ((tabsCount - 1) * controlWidth) - lefRightPadding);
        
        
        controlCopyWidth = w - lefRightPadding - (tabsCount * controlWidth)
	
        $("#A .copy").width(controlCopyWidth);
        $("#B .copy").width(controlCopyWidth);
        $("#C .copy").width(controlCopyWidth);
        $("#D .copy").width(controlCopyWidth);
        $("#E .copy").width(controlCopyWidth);
		
		
        openImage = h - 360;
        closedImage = openImage - 220;

        $('#slfixedwidth').width(w - 180 + (offset*controlWidth));
        $('#thumbsWrapper').width(w - 180 + (offset*controlWidth));

        $('#imageWrapper').width(w - 180 + (offset * controlWidth));
        $('.mainimagearea').width(w - 180 + (offset * controlWidth));

        $('.mainimagearea').height(openImage);
        $('#imageWrapper').height(openImage);
        $('#NextDivImageWrapper').height(openImage);

        $('.scroll-pane').jScrollPane({ showArrows: true });
        $("#chronologyContainer .jspContainer").attr("style", "")
        $('.chronology-pane').jScrollPane({ showArrows: true });
        $('.captioncontainer').hide();


    } else if (pageType == "feature") {

        $('.footer').css("position", "fixed");
        $('.footer').css("bottom", "0px");
        $('.footer').css("width", (w - 40) + "px");


        openImage = h - 360;

		$('.captioncontainer').css("top", (openImage + 185) + "px");
		$('#imageWrapper').width(w - 120);
	    $('.mainimagearea').width(w - 120);
		

		$('.mainimagearea').height(openImage);
		$('#imageWrapper').height(openImage);
		$('#NextDivImageWrapper').height(openImage);

		$('.righthandrows .leftfade').height(h - 220);
		$('.dottedborderleft').height(h - 200);
        $('.featurerighttb').height(h - 220);

        if (newsOpen == false) {
            $('#tslfixedwidth').width(w - 120);
            $('#slfixedwidth').width(w - 120);
        } else {
            $('#tslfixedwidth').width(w - 120 - 285);
            $('#slfixedwidth').width(w - 120 - 285);
        }


    } else if (pageType == "fullScreen") {


        $('.footer').css("position", "fixed");
        $('.footer').css("bottom", "0px");
        $('.footer').css("width", (w - 40) + "px");

        openImage = h - 164;

		$('.captioncontainer').css("top", (openImage + 185) + "px");

		$('#imageWrapper').width(w - 120);
		$('.mainimagearea').width(w - 120);

		$('.mainimagearea').height(openImage);
		$('#imageWrapper').height(openImage);

		$('.righthandrows .leftfade').height(h - 220);
		$('.dottedborderleft').height(h - 200);
        $('.featurerighttb').height(h - 220);


        $('#tslfixedwidth').width(w - 120);
        $('#slfixedwidth').width(w - 120);
   


    } else if (pageType == "enquiries") {

        var w = $(window).width();
        var h = $(window).height();

        $('.footer').css("position", "fixed");
        $('.footer').css("bottom", "0px");
        $('.footer').css("width", (w - 40) + "px");
       

        $('.newsmaincontent').height(h - 220);
        $('.righthandrows .leftfade').height(h - 220);


        if (loadedEnquiries == false) {

            $('.righthandrows .featurerighttc').height(h - 220);
            $('.featurerighttc').width((($(window).width() - 40) / 3) - 57)
 
            $('#newsControls #openNews img').attr("src", "/Images/reportage/reportagelayout/small-arrow-left-grey.gif")
            $('#newsControls #closeNews img').attr("src", "/Images/reportage/reportagelayout/small-arrow-right-white.gif")

            $('.righthandrows').stop();
            $('.righthandrows').animate({
                width: ($(window).width() - 40) / 3
                }, { queue: false, duration: openSpeed, easing: "quartEaseInOut", complete: function () {

                    copyEnqOpen = true;
                    loadedEnquiries = true;
                    $('.enquiries-pane').jScrollPane({ showArrows: true });


                }
            });
        } else {
//           // $('.righthandrows .jspContainer').attr("style", "")
//            $('.righthandrows .featurerighttc').height(h - 220);
//            $('.featurerighttc').width((($(window).width() - 40) / 3) - 57)

//            setTimeout('$(".enquiries-pane").jScrollPane({ showArrows: true });', 1000);
//        

        }

     
     

    } else if ((pageType == "news") || (pageType == "terms") || (pageType == "archive") || (pageType == "multimedia") || (pageType == "features") || (pageType == "search")) {

        $('.footer').css("position", "");
        $('.footer').css("bottom", "");
        $('.footer').css("width", (w - 40) + "px");
       
       
        $(".websitetrmscont").width(700);        
        $(".newsmaincontent").css("height", "");
		//alert($(".newsmaincontent").height() + " " + h);
        if ($(".newsmaincontent").height() < (h - 220)) {
            $(".newsmaincontent").css("height", (h - 220) + "px")
        }
    }
}


//util

Array.prototype.shuffle = function () {
    var s = [];
    while (this.length) s.push(this.splice(Math.random() * this.length, 1));
    while (s.length) this.push(s.pop());
    return this;
}

String.prototype.startsWith = function (str)
{ return (this.match("^" + str) == str) }


String.prototype.endsWith = function (str)
{ return (this.match(str + "$") == str) }

String.prototype.trim = function () {
    return
    (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""))
}
