﻿var topicPostBackScript = null ;
var continentPostBackScript = null ;
var typePostBackScript = null ;

$(function(){

    var showMenu = function() {
        var toolPanel = $(this).children(".toolPanel") ;
        if(toolPanel.is(":hidden"))
        {
            toolPanel.show();
        }
    };
     
    var hideMenu = function() {
        var toolPanel = $(this).children(".toolPanel") ;
        toolPanel.hide();
    };


    $(".toolPanel").hide() ;
    $(".toolPanel").parent().hoverIntent(showMenu, hideMenu) ;

    $(".overrideExpandableList").click( function(){
        var hrefValue = $(this).attr('href') ;
        window.location.href = hrefValue;
        }) ;
        
    prepareCaseStudyControls('.topicBoxes' , '.submitTopicChanges' , topicPostBackScript) ;
    prepareCaseStudyControls('.continentBoxes' , '.submitContinentChanges' , continentPostBackScript) ;
    prepareCaseStudyControls('.typeBoxes' , '.submitTypeChanges' , typePostBackScript) ;
    
    
    
    
    
 /*
    Page and Web Properties Related functions BEGIN
 */   


	$('.btnDefaultOrder').click(function(){
        var control = $(".txtDefaultOrderText");
        control[0].value = "RightHandFreeTextName;ChangeCountryName;KeyName;RelatedDocumentsName;QuickLinksName;LocalWebsitesName;RelatedVideosName;RightHandBlankSpaceName;BrandsRightHandTextName;NutritionalCompasRightHandTextName;BrandsAToZName;EnjoymentWellbeingName;";
        return false;
    });

 /*
    Page and Web Properties Related functions  END
 */       
   
}) ;



function prepareCaseStudyControls( checkboxes, buttonReference , callbackReference)
{

    $(checkboxes + 'ChangesAlert').hide() ;
    
    $(checkboxes).each(function() { 
            $(this).attr('defValue' , $(this).is(":checked") ) ;
            $(this).attr('defCSSBackground' , $(this).css('background') ) ;
            $(this).attr('defCSSBorder' , $(this).css('border') ) ;
         }) ;
    

    $(checkboxes).click( function() {
    var checkedCondition = $(this).is(":checked") ;
    var defaultCondition = ($(this).attr('defValue') == 'true') ? true : false  ;
    
    var userChecked = (checkedCondition && (! defaultCondition )) ;
    var userUnchecked = ( defaultCondition  && (!checkedCondition) ) ;
    
    if ( userChecked )
    {
        $(this).css({background:"yellow", border:"3px red solid"});
        $(this).attr('IsDirty' , '1') ;
    }
    else
    { 
        if ( userUnchecked )
        {
            $(this).css({background:"yellow", border:"3px red solid"});
            $(this).attr('IsDirty' , '1') ;
        }
        else
        {
            $(this).css( 'background', 'white' ) ;
            $(this).css( 'border', '0px white solid' ) ;
            $(this).attr('IsDirty' , '0') ;
        }
    }
    
    var hasChanges = false ;
    $(checkboxes + '[IsDirty]').each(function(){ 
            if ($(this).attr('IsDirty') == '1') 
            {
                hasChanges =  true ;
            }
        }) ;
    
    if ( hasChanges )
        $(checkboxes + 'ChangesAlert').show() ;
    else
        $(checkboxes + 'ChangesAlert').hide() ;
}) ;


    $(buttonReference).click(function(){
        var selectedValues = [];
        
        var finalisedPostBackString = callbackReference.replace("{0}" , GetSelectedValues(checkboxes));
        eval( finalisedPostBackString ) ;
        return false ;
    }) ;

}



function GetSelectedValues( controlReference )
{
    var selectedValues = [];
    $(controlReference).each(function(){
        if ( $(this).is(':checked'))
        {
            selectedValues.push( $(this).val() ) ;
        }
    }) ;
    
    return selectedValues.join(';') ;
}

