/**
 * Candy & Candy
 * 
 * @author Babu Arumugam
 * @copyright � 2011, Candy & Candy, All rights reserved.
 */

/**
 * @class Class containing all utility methods.
 * @static
 */
var prevIndex = "";
var Util = {         
    /**
	 * @method
	 * 
	 */	 
    init : function(){
        $(".submitForLandingPage").live('click', function() {
	    var dataToSend = '';
	    var errorFlag = 1;
	    var selectedId = $(this).attr('id');
	    //alert($(this).parents("div:first").find(".formFields").length);
	    $(this).parents("div:first").find(".formFields").each(function(){
		if($(this).find('input').val() != 'Submit') {
		    if($(this).find('input').val() == '') {
			errorFlag = 1;
			alert('Please enter all the values and press submit.');
			return false;
		    } else {
			errorFlag = 0;
			if(($(this).find("select").length)){
			    if($(this).find('select').val() != -1) {
				dataToSend += $(this).find('label').text() + "|" + $(this).find('select option:selected').val() + "^:^";
			    } else {
				errorFlag = 1;
				alert('Please select nature of Inquiry.');
				return false;
			    }
			} else {
			    dataToSend += $(this).find('label').text() + "|" + $(this).find('input').val() + "^:^";
			}
		    }
		}
	    });
	    if(!errorFlag) {
		$(this).val('Submitting...');
		$.post("submitForLandingPage.php", {'data': dataToSend, 'id': selectedId},
		    function(data) {
			if(data == 'true') {
			    window.location = 'homepage.php';
			    //$.modal.close();
			} else if(data == 'false') {
			    alert('Invalid username/password. Please try again.');
			} else {
			    $("#simplemodal-container").css({'height':'100px'});
			    $(".registerContainerForForm").html("<div style='color:#fff; margin:5% 0 0 0'>" + data + "</div>");
			    //alert(data);
			    //$.modal.close();
			}
		    });
	    }
	});
	 $("#registerForBrodinDesign, #signInForBrodinDesign").live('click', function(){
	    $("." + $(this).attr('id')).modal();
	    return;
	 });
	 
         $("#submit").live('click',function(){
                  $.post("processsubmit.php", {
                      formname:$("#formname").val(),
                      formemail:$("#formemail").val(),
                      formphone:$("#formphone").val(),
                      formaddress:$("#formaddress").val(),
                      formcomments:$("#formcomments").val()
                  },
                  function(data) {
                      $(".pageContent").html(data);
                  });
        });
         
        document.getElementById('nav-container').style.display = 'none';
        $(".masterMenuClass").live('click', function(){
                  document.getElementById('nav-container').style.display = 'none';
                  $(".articleTopicsWouldComeHere").html('');
                  $("#containerNews").html('');
                  if((($(this).text()).toLowerCase()) != 'home') {
                           document.getElementById('content').style.display = 'none';
                           $("#headerimgs").hide();
                  } else {
                           document.getElementById('content').style.display = 'block';
                           $("#headerimgs").show();
                  }
            $(this).parents("ul:first").find("a").removeClass("active");
            $(this).addClass('active');
            var currentId = $(this).attr("id");

            $(".containerSubMenusToWatch").hide('fast');
            if(document.getElementById("containerSubMenuFor" + currentId).style.display == 'block') {
                $("#containerSubMenuFor" + currentId).hide('fast');
            } else {
                $("#containerSubMenuFor" + currentId).css("left","358px").show('fast');
                $(".classFor"+currentId+":first").trigger("click");
            }
        });
        

        
        $(".classForResidential, .classForCommercial, .classForMarine, .classForNews_and_Press").live('click', function(){
                  document.getElementById('nav-container').style.display = 'none';
                  $(this).parents("ul:first").find("a").removeClass("active");
                  $(this).addClass('active');
                  
                  $.post('getImagesForThis.php',{
                      'id': $(this).attr('id')
                  },function(data) {
		    if(data == 'false') {
			window.location = 'index.php';
                        return false;
		    } else {
			document.getElementById('content').style.display = 'block';
			$("#containerNews").html(data);
		     
			if($("#containerNews").find(".checkForPress").length) {
				 var getArticleHtml = $("#article").html();
				 $("#article").remove();
				 $(".articleTopicsWouldComeHere").html("<div id='article'>"+getArticleHtml+"</div>")
				 if($("#containerNews").find(".news").length > 0) {
					  document.getElementById('nav-container').style.display = 'block';
					  showNews(0);
				 }
			} else {
			    $("a[rel='example1']").colorbox({
				slideshow:true
			    });
			}   
		    }
                  }, 'html');
        });
        
        
        

        $(".masterSubMenuClass, .actionClassForNews_and_Press").click(function(){
            document.getElementById('nav-container').style.display = 'none';
            $(this).parents("ul:first").find("a").removeClass("active");
            $(this).addClass('active');
        });


        $(".classForCandyAndCandyDesign").live('click', function(){
            $.post('getImagesForThis.php',{
                'folder': $(this).text()
            },function(data){
		if(data == 'false') {
		    window.location = 'index.php';
		    return false;
		} else {
		    document.getElementById('content').style.display = 'block';
		    $("#containerNews").html(data);
		    $("a[rel='example1']").colorbox({
			slideshow:true
		    });
		}
            }, 'html');
        });
                  
        $(".actionClassForNewsAndPress").live('click', function(){
            document.getElementById('nav-container').style.display = 'block';
            $.post('getNewsForThis.php',{
                'news': $(this).text()
            },function(data){
                document.getElementById('content').style.display = 'block';
                $("#containerNews").html(data);
                showNews(0);
            }, 'html');
        });
	$("#containerMenu ul:first li:last").live('click', function(){
	    if($(this).attr('class') != 'customHomepageMenu') {
		$.post('getImagesForThis.php',{
		    'text': $(this).text()
		},function(data){
		    if(data == 'false') {
			window.location = 'index.php';
                        return false;
		    } else {
			document.getElementById('content').style.display = 'block';
			document.getElementById('nav-container').style.display = 'none';
			$("#containerNews").html(data);
		    }
		}, 'html');
	    }
        });
                  
        $("#CandyAndCandyDesign").toggle(
            function(){
                $("#containerSubMenuForCandyAndCandyDesign").css("left","358px").show('fast');
                $(".classForCandyAndCandyDesign:first").trigger("click");
            }, function(){
                $("#containerSubMenuNewsAndPress").hide('fast');
                $("#containerSubMenuForCandyAndCandyDesign").hide('fast');
            });
        $("#newsandpress").toggle(
            function(){
                $("#containerSubMenuForCandyAndCandyDesign").hide('fast');
                $("#containerSubMenuNewsAndPress").css("left","358px").show('fast');
            },
            function(){
                $("#containerSubMenuNewsAndPress").hide('fast');
            }
            );
		 
        $("#previous_article").click(function(){
            var currIndex = document.getElementById('currIndex').value;
            showNews(currIndex, 'prev');
        });
        $("#viewindex").click(function(){
            var toHeight = ($("#article")[0].clientHeight == 530)? 0 : 530;
            $("#article").animate({
                height: toHeight
            }, 500, function() {  });
        });
        $("#nextarticle").click(function(){
            var currIndex = document.getElementById('currIndex').value;
            showNews(currIndex, 'next');
        });
        $(".news").click(function(){
            var obj = $(this);
            obj[0].children[1].value;
            debugger;
            var currIndex = obj[0].children[1].value;
            showNews(currIndex);
        });
		 

    },
    pngfix : function(){
        $(document).ready(function(){
            $(document).pngFix();
        });
    },
    gotoNews : function(i){
        $("#article").animate({
            height: 0
        }, 500, function() {
            showNews(i)
        });
    }
};

function showNews(currIndex, btn){
    var newsTotal = $("#containerNews").find('.news').length;
    var newsWidth = $('.news')[0].clientWidth;
    if(btn){
        var prevNews = $('div.news')[currIndex];
        $(prevNews).children('.alpha').addClass('active');
        if(btn == 'prev'){
            if(currIndex < newsTotal){
                if(currIndex != 0){
                    currIndex--;
                }
            }
        }
        else {
            if(currIndex < newsTotal-1){
                currIndex++;
            }
        }
    }
    else{
        var prevNews = $('div.news')[prevIndex];
        $(prevNews).children('.alpha').addClass('active');
    }
    var currNews = $('div.news')[currIndex];
    $(currNews).children('.alpha').removeClass('active');
    var newPos = -(currIndex*newsWidth);
    document.getElementById('currIndex').value = currIndex;
    $('#containerNews').animate({
        left: newPos
    }, 500, function() { });
    prevIndex = currIndex;
}

