﻿/* All the code below is bespoke and Copywright 2010 of RedStar Creative Limited */
/* For useage rights and more information please visit www.redstarcreative.co.uk */

function initPage() {

    /* Initialise our menu for the child sub-sections in the portfolio */
    $('ul.child-2').hide();
    $('ul.child-1').parent().hide();
    $('li.focusimg').hide();
    $('li.redstar').hide();
    $('li.focusimg').addClass('hidden');
        
    /* Bind an action so that each of our sub-menu headers opens the relevant items */
    $('ul.child-1 li.option').bind('click', function(event) {
        var extendMe = 'li.' + $(this).attr('class').split(' ')[0];
        $('ul.child-2').slideUp('slow');
        $(extendMe).parent().slideDown('slow');
        event.preventDefault();
    });

    $('#flash').flash({
        src: '/assets/swf/homeflash.swf',
        width: 650,
        height: 700,
        wmode: 'transparent'

    });

    $(".haccordion").haccordion();


    //$(document).pngFix(); 




    // When everything has loaded, place all polaroids on a random position
    $(".polaroid").each(function(i) {
        var tempVal = Math.round(Math.random());
        if (tempVal == 1) {
            var rotDegrees = randomXToY(330, 360); // rotate left
        } else {
            var rotDegrees = randomXToY(0, 30); // rotate right
        }

        // Internet Explorer doesn't have the "window.innerWidth" and "window.innerHeight" properties
        if (window.innerWidth == undefined) {
            var wiw = 650;
            var wih = 700;
        } else {
            var wiw = 650;
            var wih = 700;
        }

        var cssObj = { 'right': Math.random() * (wiw - 400),
            'bottom': Math.random() * (wih - 400),
            '-webkit-transform': 'rotate(' + rotDegrees + 'deg)',  // safari only
            'tranform': 'rotate(' + rotDegrees + 'deg)'
        }; // added in case CSS3 is standard
        $(this).css(cssObj);
    });

    // Set the Z-Index (used to display images on top while dragging)
    var zindexnr = 1;

    // boolean to check if the user is dragging
    var dragging = false;

    // Show the polaroid on top when clicked on
    $(".polaroid").mouseup(function(e) {
        if (!dragging) {
            // Bring polaroid to the foreground
            zindexnr++;
            var cssObj = { 'z-index': zindexnr,
                'transform': 'rotate(0deg)',  // added in case CSS3 is standard
                '-webkit-transform': 'rotate(0deg)'
            };  // safari only
            $(this).css(cssObj);
        }

        var polTitle = $(".poltitle", this).html();
        var polText = $(".poltext", this).html();
        var setText = "<strong>" +polTitle + "</strong>" + polText;

        $(".text").html(setText);

    });

    // Make the polaroid draggable & display a shadow when dragging
    $(".polaroid").draggable({
        cursor: 'crosshair',
        start: function(event, ui) {
            dragging = true;
            zindexnr++;
            var cssObj = { 'box-shadow': '#888 5px 10px 10px', // added in case CSS3 is standard
                '-webkit-box-shadow': '#888 5px 10px 10px', // safari only
                'margin-left': '-10px',
                'margin-top': '-10px',
                'z-index': zindexnr
            };
            $(this).css(cssObj);
        },
        stop: function(event, ui) {
            var tempVal = Math.round(Math.random());
            if (tempVal == 1) {
                var rotDegrees = randomXToY(330, 360); // rotate left
            } else {
                var rotDegrees = randomXToY(0, 30); // rotate right
            }
            var cssObj = { 'box-shadow': '', // added in case CSS3 is standard
                '-webkit-box-shadow': '', // safari only
                'transform': 'rotate(' + rotDegrees + 'deg)', // added in case CSS3 is standard
                '-webkit-transform': 'rotate(' + rotDegrees + 'deg)', // safari only
                'margin-left': '0px',
                'margin-top': '0px'
            };
            $(this).css(cssObj);
            dragging = false;
        }
    });
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
	
}



/* Sample call: presetNavigation('10'); */
/* Set a default item on the navigation */
function presetNavigation(itemname) {
    
    
    itemname = 'psid' + itemname;

    var extendMe = 'li.' + itemname;
    $('ul.child-2').slideUp('slow');
    $(extendMe).parent().slideDown('slow');

    
}

/* Set the preview image to the relevant option */
function setRolloverOption(imageUrl) {

    $('li.focusimg img').attr('src', imageUrl);
    
    if($('li.focusimg').attr('class')=='focusimg hidden')
    {
        $('li.focusimg').slideDown('slow');
        $('li.focusimg').removeClass('hidden');
    }
}


// Function to get random number upto m
// http://roshanbh.com.np/2008/09/get-random-number-range-two-numbers-javascript.html
function randomXToY(minVal, maxVal, floatVal) {
    var randVal = minVal + (Math.random() * (maxVal - minVal));
    return typeof floatVal == 'undefined' ? Math.round(randVal) : randVal.toFixed(floatVal);
}